使用回形针在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 %>
答案 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>