嵌套Form_for不显示fields_for的字段

时间:2013-03-31 13:09:35

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1 ruby-on-rails-3.2

我的观点是

<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>

控制器

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

和模型是:

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

但我的观点并未显示c.text_field :company_name。 请帮助我,我错了,并纠正他们

1 个答案:

答案 0 :(得分:0)

替换

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

通过

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