Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

You can access the name of the uploaded file with the helper original_filename. so params[:model][:file].original_filename

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top