Rails嵌套表单正在翻倍

时间:2015-02-25 23:36:37

标签: ruby-on-rails ruby-on-rails-4 nested-attributes

使用回形针在rails 4应用程序中使用嵌套表单,并且当我编辑帖子时图像(:img)加倍。例如,在show 2中有一个图像结果,但数据库只显示一个条目。

_form.html.erb

<fieldset>
 <%= f.fields_for :images do |builder| %>
  <%= render 'image_fields', f: builder %>
 <% end %>
</fieldset>
<%= link_to_add_fields "Add New Image", f, :images %>

_image_fields.html.erb

<% @article.images.each do |a| %>
<div class="form-group">        
 <fieldset>
  <div class="col-md-2">
   <%= image_tag a.img(:thumb) %>
  </div>
 <div class="col-md-10">
 <%= f.input :img, label: false  %>
 </div>
</fieldset>
</div>
<% end %>

1 个答案:

答案 0 :(得分:0)

试试这个:

_form.html.erb

 <fieldset>
   <% @article.images.each do |image| %>
     <%= f.fields_for :images, image do |builder| %>
       <%= render partial: 'image_fields', locals: { f: builder, image: image } %>
     <% end %>
   <% end %>
</fieldset>
<%= link_to_add_fields "Add New Image", f, :images %>

_image_fields.html.erb

<div class="form-group">        
 <fieldset>
  <div class="col-md-2">
   <%= image_tag image.img(:thumb) %>
  </div>
 <div class="col-md-10">
 <%= f.input :img, label: false  %>
 </div>
</fieldset>
</div>