Plural for fields_for has_many association not showing in view

11,732

Solution 1

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/

Solution 2

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

Share:
11,732
8bithero
Author by

8bithero

http://www.8bithero.io

Updated on June 11, 2022

Comments

  • 8bithero
    8bithero almost 2 years

    Currently, an Item belongs_to a Company and has_many ItemVariants.

    I'm trying to use nested fields_for to add ItemVariant fields through the Item form, however using :item_variants does not display the form. It is only displayed when I use the singular.

    I have check my associations and they seem to be correct, could it possibly have something to do with item being nested under Company, or am I missing something else?

    Thanks in advance.

    Note: Irrelevant code has been omitted from the snippets below.

    EDIT: Don't know if this is relevant, but I'm using CanCan for Authentication.

    routes.rb

    resources :companies do
      resources :items
    end
    

    item.rb

    class Item < ActiveRecord::Base
      attr_accessible :name, :sku, :item_type, :comments, :item_variants_attributes
    
    
      # Associations
      #-----------------------------------------------------------------------
      belongs_to :company
      belongs_to :item_type
      has_many   :item_variants
    
      accepts_nested_attributes_for :item_variants, allow_destroy: true
    
    end
    

    item_variant.rb

    class ItemVariant < ActiveRecord::Base
      attr_accessible :item_id, :location_id
    
      # Associations
      #-----------------------------------------------------------------------
      belongs_to :item
    
    end
    

    item/new.html.erb

    <%= form_for [@company, @item] do |f| %>
      ...
      ...
      <%= f.fields_for :item_variants do |builder| %>
        <fieldset>
          <%= builder.label :location_id %>
          <%= builder.collection_select :location_id, @company.locations.order(:name), :id, :name, :include_blank => true %>
        </fieldset>
      <% end %>
      ...
      ...
    <% end %>
    
  • 8bithero
    8bithero over 11 years
    Thanks! Worked perfectly,, but as a reference I was using the RailsCast ep 196-Nested-model-form-revised. In his new action it only contained @survey = Survey.new with no building of the association. Any idea why I need to build the association and Ryan didn't/doesn't?
  • 8bithero
    8bithero over 11 years
    Thanks. Got it working, but using @item.item_variants.build if @item.item_variants.nil? didn't work. It only works if i remove the if statement. Also, Using that Revised version of your link was what confused me. In his controller he doesn't use .build. He only creates the @survey instance. Any idea why I need to build the association and Ryan didn't/doesn't?
  • rewritten
    rewritten over 11 years
    That railscast is even better, although complex, because it can dynamically add questions in the survey form. So it does not need to prepopulate anything.
  • Jordan Michael Rushing
    Jordan Michael Rushing almost 10 years
    I just wanted to add on some meaning for those who are confused about the .build and why Ryans' didn't need it. If you look at his code in appliation_helper on line 3 its creating the class instance. So that takes the place of .build. Hope this helps anyone else out there!