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?