Rails回形针 - 附加多个图像

时间:2012-12-16 17:05:48

标签: ruby-on-rails image paperclip attachment

我正在尝试用回形针将多个图像附加到组件上。 正如本教程 http://www.tkalin.com/blog_posts/multiple-file-upload-with-rails-3-2-paperclip-html5-and-no-javascript

我有以下文件:

_form.html.erb

<div class="field">
   <%= file_field_tag('component_images_photo', multiple: true, name: "component[images_attributes][][photo]") %>
</div> 
<div class="actions">
   <%= f.submit %>
</div>

component.rb

has_many :images
accepts_nested_attributes_for :images

image.rb

class Image < ActiveRecord::Base
    attr_accessible :photo_file_name

    belongs_to :component
    has_attached_file :photo
end

图像模型已创建为:

rails generate model image

回形针附件:

rails g paperclip image photo

此时我在尝试创建组件时遇到以下错误:

Can't mass-assign protected attributes: image_attributes

也许我应该忘记模型和回形针调用中的某些内容?

修改 我应该将图像模型创建为:

rails generate model image component:references

...

1 个答案:

答案 0 :(得分:0)

您必须将attr_accessible :images_attributes添加到component.rb