File upload in Rails- data is an object, how do I return it in my view?
When doing an upload in my Rails project, the database stores
--- !ruby/object:File 开发者_运维问答 content_type: application/octet-stream original_path: my.numbers
how do I get it to return my.numbers in my view only?
Thanks a bunch! Marco
ps. I don't want to use attachment_fu or any other plugin preferably.
A file upload is actually received by your controller as a File object, not as data, so it is your responsibility to read it in. Typically uploaded files are saved in a temporary directory and an open filehandle to it is present in the params.
You could do something like the following to retrieve the data:
def create
# Read in data from file into parameter before creating anything
if (params[:model] and params[:model][:file])
params[:model][:file] = params[:model][:file].read
end
@model = MyModel.create(params[:model])
end
You would probably need to be sure that the column in the database can store binary data. In MySQL migrations this is the :binary column type.
You can access the name of the uploaded file with the helper original_filename. so params[:model][:file].original_filename
精彩评论