0

This is my nested_form:

..
...
 54   <div>
 55     <h2> Address </h2>
 56     <%= f.fields_for :address do |address_form| %>
 57       <%= address_form.text_field :country %>
 58     <% end %>
 59   </div>
 60
 61   <div>
 62     <h2> Participants </h2>
 63     <%= f.fields_for :participants do |participant_form| %>
 64       <%= participant_form.text_field :name %>
 65       <%= participant_form.link_to_remove "Remove this participant" %>
 66     <% end %>
 67     <p><%= f.link_to_add "Add a participant", :participants %></p>
 68   </div>
...
..

Now when I visit my model/new page it does not render any fields for address or participants.

This is my model:

  1 class CompetitionEntry < ActiveRecord::Base
  2   has_many :participants
  3   has_one :address
  4   has_many :music_programs
  5
  6   accepts_nested_attributes_for :address
  7
  8   accepts_nested_attributes_for :participants, :music_programs,
  9     :allow_destroy => true,
 10     :reject_if     => :all_blank
 11 end

This is my controller:

 16   def new
 17     @competition_entry = CompetitionEntry.new
 18   end

Why is it happening ? did I miss something?

1
  • Could it be that it tries to render existing participants which, in a new record, there are none? Commented Aug 21, 2013 at 16:34

3 Answers 3

1

Well, you have to use the build method to instantiate blank nested objects, so the view could render something.

def new
  @competition_entry = CompetitionEntry.new
  @competition_entry.address.build
  @competition_entry.participants.build
end

You can even use a loop to create more than one associated object. Like 3.times {@competition_entry.participants.build}.

Sign up to request clarification or add additional context in comments.

2 Comments

undefined method `addresses'
It is a has_one, so i believe it is just address.
1

If its has_one relationship then the proper way to create is not

 @competition_entry.address.build

it is

 @competition_entry.build_address

Comments

0

Into your CompetitionController use the builder like here:

def new
    @competition_entry = CompetitionEntry.new
    @competition_entry.build_address
    @competition_entry.participants.build
    @competition_entry.music_programs.build
end

Also, builder could doesn't know about attributes you want to transfer from the nested form into controller.

Put this into your controller.

def competition_entry_params

    params.require(:competition_entry).permit(<<competition_entry_attributes>>, address_attributes: [:country], participants_attributes: [:name], music_programs_attributes: [:something, :something_else])

end

And then use this into actions create and/or update

@competition_entry = CompetitionEntry.new(competition_entry_params)

Hope this will help.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.