Search code examples
ruby-on-railsruby-on-rails-3ruby-on-rails-3.1ruby-on-rails-3.2

Nested_for_form not displaying field for field_for


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


Solution

  • Replace

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

    by

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