0
我试图将一个动态数量的图片添加到名为“组件”的元素。 环顾四周,我创建了这个代码:Rails回形针 - 添加动态多个图像
component.rb
class Component < ActiveRecord::Base
has_many :images
accepts_nested_attributes_for :images, :allow_destroy => true
end
image.rb
class Image < ActiveRecord::Base
attr_accessible :component_id, :photo_file_size
belongs_to :component, :foreign_key => :component_id
has_attached_file :photo,
:styles => {
:thumb => "75x75>",
:small => "200x200>"
}
end
_form.html.erb
<%= form_for setup_component(@component) , :html => { :multipart => true } do |f| %>
...
<h2> PHOTOS </h2>
<% f.fields_for @component.images.build do |p| %>
<h2>Photo</h2>
<p><%= f.label :data %><br /><%= f.file_field :data %></p>
<% end %>
application_helper.rb
def setup_component(comp)
return comp.images.build if comp.images.empty?
end
所以我得到当我尝试去形成以下错误:
undefined method `images_path'
哪里'images_path'出现在你的代码? –
ID未显示。但是当我评论这一行时,问题就消失了: 'return comp.images.build if comp.images.empty?'。 现在我在控制器中这样做,它的工作原理,但是当我提交表单时,它崩溃了。所以更多的是错误的。 – user1573607
通常,'fields_for'用于关联:'fields_for @ component.images'。这有帮助吗? –