3

I'm following along with Railscast 196 in which R Bates makes nested forms. He has a Survey Model with a has_many association with a Questions model. Survey also accepts_nested_attributes_for :questions. In the new action of the surveys_controller, he does the following to create three question fields in the survey form

  def new
    @survey = Survey.new
    3.times { @survey.questions.build }
  end

and inside form_for @survey he has the following to create question fields in the form

<% f.fields_for :questions do |builder| %>

  <p>
    <%= builder.label :content, "Question" %><br />
    <%= builder.text_area :content, :rows =>  3 %>

  </p>

In the video, once he clicks on new survey, it shows the three question fields (along with other elements of the form). The question related elements of the form are not appearing for me. I think he made this episode before Rails 3 was released, so something might have changed, however, I can't figure out what except that it's I'm not seeing the results of doing 3.times (@survey.questions.build)

Model

class Survey < ActiveRecord::Base
  attr_accessible :name

  has_many :questions, :dependent => :destroy
  accepts_nested_attributes_for :questions
end

Form

<%= form_for @survey do |f| %>
  <%= f.error_messages %>
  <p>
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </p>

  <% f.fields_for :questions do |builder| %>

  <p>
    <%= builder.label :content, "Question" %><br />
    <%= builder.text_area :content, :rows =>  3 %>

  </p>
  <% end %>
  <p><%= f.submit %></p>
<% end %>

html form

<form accept-charset="UTF-8" action="/surveys" class="new_survey" id="new_survey" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;"><input name="authenticity_token" type="hidden" value="AWvA3/JpixF0C3sO8OzA5mMGsJzknvu99eovYv7M78E="></div>

  <p>
    <label for="survey_name">Name</label><br />
    <input id="survey_name" name="survey[name]" size="30" type="text">
  </p>

  <p><input name="commit" type="submit" value="Create Survey"></p>
</form>

Update

inside the form, I added this

 <%= @survey.questions %>

and it's showing this

[#<Question id: nil, survey_id: nil, content: nil, created_at: nil, updated_at: nil>, #<Question id: nil, survey_id: nil, content: nil, created_at: nil, updated_at: nil>, #<Question id: nil, survey_id: nil, content: nil, created_at: nil, updated_at: nil>]

so the 3.times in the new action of the Survey controller is obviously working, but for some reason the fields aren't displaying when I do

<% f.fields_for :questions do |builder| %>

      <p>
        <%= builder.label :content, "Question" %><br />
        <%= builder.text_area :content, :rows =>  3 %>

      </p>
1
  • is that your code? if it is, i don't see any issues with it. you should see the 3 questions on the form. can you post your survey model and the html form? Commented Feb 18, 2013 at 2:49

1 Answer 1

3

i should've seen it the first time you posted. anyway, you are missing = on your fields_for. that should be

<%= f.fields_for :questions do |builder| %>
Sign up to request clarification or add additional context in comments.

2 Comments

It's always the little details that get us.
This is actually something that appears to have changed from Rails 2 to 3. Ryan Bates source code doesn't include the '='

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.