Leahcim
Leahcim

Reputation: 41919

Nested models form Rails 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>

Upvotes: 3

Views: 1243

Answers (1)

jvnill
jvnill

Reputation: 29599

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| %>

Upvotes: 3

Related Questions