Rails 3.1 + Paperclip + jQuery fileupload

自闭症网瘾萝莉.ら 提交于 2019-12-02 17:20:20

After struggling with this problem for a while I discovered the problem appeared when :multipart => true is added to the f.file_field since the form field name changes from user[photo] to user[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 used form_field_tag 'user[photo]', :multiple => true in the view.

My code looks like this:

## app/model/user.rb
has_attached_file :photo

def to_fileupload_json
  {
    "name" => photo_file_name,
    "size" => photo_file_size,
    ...
  }
end

## app/views/photos/new.html.haml
= form_for @user, :url => users_path, :html => { :multipart => true } do |f|
  #fileupload
    = file_field_tag 'user[photo]', :multiple => true
    = f.submit

= javascript_include_tag "jquery.fileupload.js"
# require all other JS files needed for the plugin (jQuery, jQuery UI, ...)
= stylesheet_link_tag "jquery.fileupload-ui.css"
= render :partial => "jquery_file_templates" # partial with jQuery templates for responses 

:javascript
  $(function () {
      uploader = $('#fileupload').fileupload()
  }


## app/controllers/users_controller.rb
def create
  @user = User.create params[:user]
end

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:

  • add accepts_nested_attributes_for :photos to your (User) model
  • add a method photos_attribues=(attributes) to your (User) model and handle record creation there
  • build record for photos 3.times { @user.photos.build } in User's new method

Example:

def photos_attribues=(attributes)
  attributes.each do |key, value|
    photo = Photo.create :photo => value, ...
  end
end

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:

f.file_field :photo, multiple: true, name: 'user[photo]'

Rails will generate user[photo] instead of user[photo][].

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!