Plural for fields_for has_many association not showing in view

别等时光非礼了梦想. 提交于 2019-12-05 10:36:53

You should prepopulate @item.item_variants with some data:

def new # in the ItemController
  ...
  @item = Item.new
  3.times { @item.item_variants.build }
  ...
end

Source: http://rubysource.com/complex-rails-forms-with-nested-attributes/

try this way

in your item controller new action write

def new
  ...
    @item = # define item here
    @item.item_variants.build if @item.item_variants.nil?
  ...
end

and in item/new.html.erb

<%= form_for @item do |f| %>
  ...
  ...
  <%= f.fields_for :item_variants do |builder| %>
    ...
  <% end %>
  ...
  ...
<% end %>

for more see video - Nested Model Form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!