RailsCasts Pro episodes are now free!

Learn more or hide this

dedgars's Profile

GitHub User: dedgars

Comments by

Avatar

my mistake was that I did not add 'survey_id:integer' while generating questions model. it solved the problem

Avatar

Hi! Please help!
I am stuck on this error:

ActiveRecord::UnknownAttributeError in SurveysController#new
unknown attribute: survey_id

Rails 3.2.2

models/survey.rb
class Survey < ActiveRecord::Base
  has_many :questions, :dependent => :destroy
  accepts_nested_attributes_for :questions, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true
end
models/question.rb
class Question < ActiveRecord::Base
  belongs_to :survey
  has_many :answers, :dependent => :destroy
  accepts_nested_attributes_for :answers, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true
end
models/answer.rb
class Answer < ActiveRecord::Base
  belongs_to :question
end
```surveys_controller.rb
...
def new
  @survey = Survey.new
  3.times do
    question = @survey.questions.build
    4.times { question.answers.build }
  end
end
...
_form.html.erb
<%= form_for @survey do |f| %>
  <%= f.error_messages %>
  <p>
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </p>
  <%= f.fields_for :questions do |builder| %>
    <%= render "question_fields", :f => builder %>
  <% end %>
  <p><%= f.submit "Submit" %></p>
<% end %>