Nested_for_form不显示field_for的字段

时间:2021-10-10 14:32:10

MY view is

我的观点是

<h3> Register New Items </h3>
<div class="row">
<div class="span2 offset4">
<%= nested_form_for @item_categories, :url => items_path, :method => :post, :html => { :class => "item_category"} do |f| %>
        <div><%= f.label :item_name %>
        <%= f.text_field :item_name %></div>
        </br>
         <% f.fields_for :item_companies do |c| %>
              <%= c.text_field :company_name %></div>
         <%end%>
        <div><%= f.submit "Submit" %></div>
    <% end %>
</div>

Controller is

控制器是

@item_categories = ItemCategory.new
  3.times do
    item_company = @item_categories.item_companies.build
    4.times { item_company.item_weights.build }
 end

and models are:

和模型是:

class ItemCategory < ActiveRecord::Base
  attr_accessible :item_name
  has_many :item_weights
  has_many :item_companies#, :through=> :item_weights
  accepts_nested_attributes_for :item_companies
end


class ItemCompany < ActiveRecord::Base
  attr_accessible :company_name, :item_category_id

  has_many :item_weights
  has_many :item_categories#, :through=> :item_weights
end

class ItemWeight < ActiveRecord::Base
  attr_accessible :item_category_id, :item_company_id, :weight
  belongs_to :item_company
  belongs_to :item_category
end

But my view is not displaying c.text_field :company_name in view. Please help me where i am wrong and correct them

但我的观点是没有在视图中显示c.text_field:company_name。请帮助我,我错了,纠正他们

1 个解决方案

#1


0  

Replace

更换

<% f.fields_for :item_companies do |c| %>

by

通过

<%= f.fields_for :item_companies do |c| %>

#1


0  

Replace

更换

<% f.fields_for :item_companies do |c| %>

by

通过

<%= f.fields_for :item_companies do |c| %>