Shpigford
Shpigford

Reputation: 25338

accepts_nested_attributes_for keeps form fields from showing

When I use accepts_nested_attributes_for the corresponding fields no longer show in my view.

class Survey < ActiveRecord::Base
  has_many :questions   
  accepts_nested_attributes_for :questions
end

class Question < ActiveRecord::Base
  belongs_to :survey
end

Then in my view:

<%= form_for @survey do |f| %>
  <%= f.fields_for :questions do |question_fields| %>
    <%= question_fields.text_area :text %> 
  <% end %>
<% end %>

If I remove accepts_nested_attributes_for then the text_area shows, but if I keep it...nothing gets renders.

I'm running Rails 3.0.3

Upvotes: 1

Views: 1245

Answers (2)

Rishav Rastogi
Rishav Rastogi

Reputation: 15492

Did you build the questions , in the controller ?

Something like

@survey.questions.build

This builds one related question, so only one text area will show up. run it in a loop like

2.times { @survey.questions.build }

It will appear 2 times.

Upvotes: 4

Pan Thomakos
Pan Thomakos

Reputation: 34340

Do you want to create new questions or are you editing them? You might want to try something like this if you are creating a new question for this survey:

<= f.fields_for @survey.questions.build do |question_fields| %>

Upvotes: 2

Related Questions