简体   繁体   中英

Ruby on Rails uploading files

I get this error after trying to save a file.

undefined method `original_filename' for "Screenshot from 2017-08-26 22-12-42.png":String

def upload
  uploaded_io = params[:person][:picture]
  File.open(Rails.root.join('public', 'uploads', uploaded_io.original_filename), 'wb') do |file|
    file.write(uploaded_io.read)
  end
end

And that's my request parameters

{"utf8"=>"✓",
 "authenticity_token"=>"JqTuIpsrX/rfJ2/r74EbLjuWI/UlxpxpEWnX64ruRjO/UQfrDk/q/MhVLNOG16YJOBxmt84t4tfw4YZDFbRIzQ==",
 "description"=>{"position"=>"5", "title"=>"title", "short_description"=>"qweqwe", "long_description"=>"qweqwe"},
 "photos"=>{"photo"=>"Screenshot from 2017-08-26 22-12-42.png"},
 "button"=>""}

I recommend using carrierwave to perform file upload. The way to use it is supremely simple, and has different tools for its handling.

You can implement this gem with the following guide:

https://github.com/carrierwaveuploader/carrierwave/wiki/How-to:-use-carrierwave-with-devise

First I would like to thank @kiddorails for his help.

The documentation here is pretty clear " http://guides.rubyonrails.org/form_helpers.html#uploading-files ", the problem was in view file where I haven't put below helper inside the form,

<%= f.file_field :picture %>

as show in the documentation.

<%= form_for @person do |f| %>
<%= f.file_field :picture %>
<% end %>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM