I've been looking for a way to set up Ruby on Rails 3.1 with Paperclip and jQuery fileupload. Looking at a tutorial at jQuery fileupload page I got the system set up but I can't find a way to make paperclip process the uploaded file.
Here's what I have (in short):
# model
has_attached_file :photo ...
# view
= form_for @user, :html => {:multipart => true} do |f|
f.file_field :photo, :multiple => true
f.submit
# controller
def create
@user = User.new params[:user]
if @user.save
render :json => [...]
end
If I inspect submitted data I get all user properties in params[:user] and params[:user][:photo] which is ActionDispatch::Http::UploadedFile. When @user.save is called the image doesn't get processed or saved.
A clue, tutorial or a solution would be much appreciated!
After struggling with this problem for a while I discovered the problem appeared when
:multipart => true
is added to thef.file_field
since the form field name changes fromuser[photo]
touser[photo][]
.Using separate page to attach photos
I want to have a separate page for uploading multiple files to a record (and another one for editing User's properties). This looks to me as a temporary solution but it works. Instead of
f.form_field :photo, :multipart => true
I usedform_field_tag 'user[photo]', :multiple => true
in the view.My code looks like this:
If anyone else knows a better way (the right way) of doing this, please let us know!
Using fields_for
Depending on the structure of your app you might consider using
fields_for
.In this case you'll have to:
accepts_nested_attributes_for :photos
to your (User) modelphotos_attribues=(attributes)
to your (User) model and handle record creation there3.times { @user.photos.build }
in User's new methodExample:
Disclaimer: The code above was simplified/rewritten to make it easier to understand. I might've made mistakes while erasing unneeded stuff. And again - I'm not really sure this is the right way of solving this problem. Suggestions and improvements are more than welcome!
If you explicitly set the name on the
file_field
:Rails will generate
user[photo]
instead ofuser[photo][]
.