11

我在使用回形针上传多个文件时遇到问题,

我的模型就是这样

slider has_many imgarrays

imgarrays has_many imageobjects

imageobjects have_attachment(as for paperclip)

在我的其他模型上接收单个图像并使用回形针保存它没有问题,但我不确定如何处理imgarrays在多个文件上传期间由 param 返回的数组。

这是我的 Rails 服务器日志:

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\"\r\nContent-Type: image/png\r\n", @tempfile=#<File:/var/folders/2s/n9wb5x4534nfs1cbrlph32v00000gp/T/RackMultipart20120723-53499-1lyi4yf>>, #<ActionDispatch::Http::UploadedFile:0x007fb471e99dc8 @original_filename="Icon@2x.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"slider[imgarrays][]\"; filename=\"Icon@2x.png\"\r\nContent-Type: image/png\r\n", @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\"\r\nContent-Type: image/png\r\n", @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

<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 %>
4

1 回答 1

11

这是我的代码,可以很好地使用回形针上传多个文件:我们可以使用嵌套属性或使用普通的简单方法来实现。

以下代码显示了正常的方法:

用户.rb

has_many :images, :dependent => :destroy

图片.rb

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

属于_to:用户

用户/视图/new.html.erb

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

...... 
 ....

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

<% end  %>

用户控制器:

......

    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

而已。图片已上传,这可能不是好方法,但它有效。

于 2013-10-15T11:10:32.310 回答