I have rails app that I created so I could use the API portion of things. I can successfully upload a file to the DB of the rails app using curl, but I can't figure out how I could limit the filetype / content type to just CSV.
csv_file.rb #model
class CsvFile < ActiveRecord::Base
# attachment :content_type => "text/csv"
# http://ryanbigg.com/2009/04/how-rails-works-2-mime-types-respond_to/
attachment :csv, extension: "csv", content_type: "text/csv"
end
csv_files.rb #controller
class API::V1::CsvFilesController < ApplicationController
# see http://stackoverflow.com/questions/15040964/ for explanation
skip_before_filter :verify_authenticity_token
def index
@csv_files = CsvFile.all
if @csv_files
render json: @csv_files,
# each_serializer: PictureSerializer,
root: "csv_files"
else
@error = Error.new(text: "404 Not found",
status: 404,
url: request.url,
method: request.method)
render json: @error.serializer
end
end
def show
if @csv_file
render json: @csv_file,
# serializer: PictureSerializer,
root: "csv_file"
else
@error = Error.new(text: "404 Not found",
status: 404,
url: request.url,
method: request.method)
render json: @error.serializer
end
end
# POST /csv_files.json
def create
@csv_file = CsvFile.new(csv_params)
if @csv_file.save
render json: @csv_file,
# serializer: PictureSerializer,
meta: { status: 201,
message: "201 Created"},
root: "csv_file"
else
@error = Error.new(text: "500 Server Error",
status: 500,
url: request.url,
method: request.method)
render :json => @error.serializer
end
end
def update
end
def delete
end
private
def csv_params
end
end
So there ended up being a couple of problems.
First, the strong parameters in the controller should look like the following,
def csv_params
params.permit(:csv_file)
end
Secondly, I needed to add a column in a migration as such,
add_column :csv_files, :csv_file_id, :string
Finally, I was able to modify the csv_file.rb
model file and add the following line.
attachment :csv_file, extension: "csv"
As it stands now, only files with an extension of .csv
can be uploaded to the API.