1

I'm trying to understand how to nest models with rails (active records to then apply it to my project with mongodb)

I'm following this railscast tutorial:

http://railscasts.com/episodes/196-nested-model-form-part-1

but i'm stuck at the beginning when I can't display the form to add new questions to the survey.

I have set up the relationships in the models

models/survey.erb:

class Survey < ActiveRecord::Base
  has_many :questions, :dependent => :destroy
  accepts_nested_attributes_for :questions
  validates_presence_of :name
end

models/question.erb:

class Question < ActiveRecord::Base
  belongs_to :survey
  validates_presence_of :content
end

controllers/surveys_controller.rb:

def new
  @survey = Survey.new
  @survey.questions.build

  respond_to do |format|
    format.html # new.html.erb
    format.json { render json: @survey }
  end
end

views/survey/_form.html.erb"

<%= form_for(@survey) do |f| %>
  <% if @survey.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@survey.errors.count, "error") %> prohibited this survey from being saved:</h2>

      <ul>
      <% @survey.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </div>
  <h3>Add new question</h3>
  <% f.fields_for :questions do |p| %>
    <%= p.label :content, "Questions" %><br />
    <%= p.text_area :content, :rows => 3 %>
  <% end %>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

but when I try to build the nested form, it doesn't work. I don't get any errors but the form doesn't show.

Am i missing something?

3 Answers 3

1

You should be using a <%=, not a <% on your field_for line, similar to your form_for line. Otherwise, the actual nested form won't get displayed.

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

1 Comment

Thanks for that. I got confused with something that I read recently. Control logic don't need <%= in rails if it doesn't output code. So I thought that the field_for was a control thingy to output the inner code :(
1

You are not taking full advantage of the nested forms in your example.

To DRY things up you should replace the form_for(@survey) by nested_form_for(@survey)

and then you can pull out the questions sub-form into it's own partial, and just do a:

f.fields_for :questions   # without a block

and handle the questions form under ./app/views/questions/_form.html.erb as a regular form_for(@question)

This way you don't have to duplicate code for the questions form inside your survey form.

P.S.: You will need to add "nested_form" Gem to your Gemfile.

2 Comments

but that's a gem or is part of rails?
It's a Gem, called "nested_from" -- you'll need to add it to your Gemfile
0

Just in case anyone else get's stuck on this and winds up here, like me, this issue is the missing '=' in the fields_for call's brackets: '<%= f.fields_for...'

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.