Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
307 views
in Technique[技术] by (71.8m points)

ruby on rails - Uploading multiple files with paperclip

I am having problems uploading multiple files using paper clip,

my models are as such

slider has_many imgarrays

imgarrays has_many imageobjects

imageobjects have_attachment(as for paperclip)

I have no problems receiving a single image and saving it using paperclip on my other models, but i am not sure of how to handle the array returned by imgarrays param during a multiple file upload.

Here is my rails server logs:

Started POST "/slider" for 127.0.0.1 at 2012-07-23 10:14:17 +0800
  Processing by SliderController#create as HTML
  Parameters: {"utf8"=>"?", "authenticity_token"=>"7HcHtSlOsU/bnxb9emhAsSl/GFBraIE6NxwijHl3REM=", "slider"=>{"question"=>"", "answer"=>"", "score"=>"", "industry_name"=>"", 
  "imgarrays"=>[#<ActionDispatch::Http::UploadedFile:0x007fb471e99f30 @original_filename="Icon.png", @content_type="image/png", @headers="Content-Disposition: form-data; name="slider[imgarrays][]"; filename="Icon.png"
Content-Type: image/png
", @tempfile=#<File:/var/folders/2s/n9wb5x4534nfs1cbrlph32v00000gp/T/RackMultipart20120723-53499-1lyi4yf>>, #<ActionDispatch::Http::UploadedFile:0x007fb471e99dc8 @original_filename="[email protected]", @content_type="image/png", @headers="Content-Disposition: form-data; name="slider[imgarrays][]"; filename="[email protected]"
Content-Type: image/png
", @tempfile=#<File:/var/folders/2s/n9wb5x4534nfs1cbrlph32v00000gp/T/RackMultipart20120723-53499-10lala2>>, #<ActionDispatch::Http::UploadedFile:0x007fb471e99d50 @original_filename="greenButton.png", @content_type="image/png", @headers="Content-Disposition: form-data; name="slider[imgarrays][]"; filename="greenButton.png"
Content-Type: image/png
", @tempfile=#<File:/var/folders/2s/n9wb5x4534nfs1cbrlph32v00000gp/T/RackMultipart20120723-53499-or2rdk>>]}, "commit"=>"Create!"}
Completed 500 Internal Server Error in 18ms

ActiveRecord::AssociationTypeMismatch (Imgarray(#70206507050500) expected, got ActionDispatch::Http::UploadedFile(#70206487229960)):
  app/controllers/slider_controller.rb:12:in `new'
  app/controllers/slider_controller.rb:12:in `create'

Rendered /Users/Kinnovate/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.1.1/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.4ms)
Rendered /Users/Kinnovate/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.1.1/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.4ms)
Rendered /Users/Kinnovate/.rvm/gems/ruby-1.9.3-p194/gems/actionpack-3.1.1/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (6.5ms)

new.html.erb for slider

<div>
<%= form_for @slider ,:url=>"/slider" , :html => { :multipart => true } do |f| %>

      <%= f.label :question , "question"%>
      <%= f.text_field :question %> </br>
        <%= f.label :answer , "answer array (comma seperated)"%>
          <%= f.text_field :answer %>   </br>
        <%= f.label :score , "score"%>
     <%= f.text_field :score %> </br>
            <%= f.label :industry_name , "industry"%>
              <%= f.text_field :industry_name %>    </br>

        <%= f.label :attachedimg , "image"%>
         <%= f.file_field :imgarrays, :multiple =>:true %>  </br>

      <%= f.submit "Create", class: "btn btn-large btn-primary" %>
    <% end %>

</div>
<%= link_to 'Cancel', slider_index_path %>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here is my code that worked well to upload multiple file using paperclip: We can achieve using nested attributes or using normal easy method.

The following code shows normal method:

User.rb

has_many :images, :dependent => :destroy

Image.rb

has_attached_file :avatar, :styles => { :medium => "300x300>" }

belongs_to :user

users/views/new.html.erb

<%= form_for @user, :html => { :multipart => true } do |f| %>

...... 
 ....

<%= file_field_tag :avatar, multiple: true %>

<% end  %>

Users_controller:

.....

    if @user.save
     # params[:avatar] will be an array.
     # you can check total number of photos selected using params[:avatar].count
      params[:avatar].each do |picture|      
    
        @user.images.create(:avatar=> picture)
        # Don't forget to mention :avatar(field name)
    
      end
    end

Thats it. images got uploaded, this may not be the good way but it works.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...