This example is kind of cliche. I am following the railscasts episode #196 (http://railscasts.com/episodes/196-nested-model-form-revised?autoplay=true) for double nested forms.
The error I get is that my accepts_nested_attributes_for command is not generating an answers_attributes for strong parameters.
Models:
class Survey < ActiveRecord::Base
has_many :questions
accepts_nested_attributes_for :questions
end
class Question < ActiveRecord::Base
belongs_to :survey
has_many :answers
has_many :users, through: :answers
accepts_nested_attributes_for :answers
end
class Answer < ActiveRecord::Base
belongs_to :question
belongs_to :user
end
Then my surveys controller:
class SurveysController < ApplicationController
def index
@survey = current_user.surveys
end
def new
@survey = Survey.new
@question = @survey.questions.build # the nested form won't show up if I don't
@answer = @question.answers.build #Not sure if I need this line. doesn't work either way.
end
def create
@survey = Survey.new(survey_params)
if @survey.save
redirect_to @survey, notice: "Survey successfully created."
else
render 'new'
end
end
# rest.. show, edit, update, destroy
private
def survey_params
params.require(:survey).permit!
end
#def survey_params
#params.require(:survey).permit(:user_id, :name, { questions_attributes: [:_destroy, :id, :survey_id, :content, { answers_attributes: [:_destroy, :id, :content, :user_id, :question_id]}]})
#end
end
The issue I'm having is this (empty form submit). When I run my real code, I get this:
{"utf8"=>"✓", "authenticity_token"=>"[token]=", "survey"=>{"user_id"=>"1", "name"=>"test", "questions_attributes"=>{"0"=>{"content"=>"test", "_destroy"=>"0"}}, "answers"=>{"content"=>"test", "_destroy"=>"0"}}, "commit"=>"Create", "action"=>"create", "controller"=>"surveys"}
Unpermitted attributes: answers
Then when I run the permit! method to test a force though I get this:
unknown attribute: answers
Rails should be looking for answers_attributes, not answers. That makes me think it's not recognizing the model. So, here is the schema.
create_table "questions", force: true do |t|
t.integer "survey_id"
t.string "content"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "answers", force: true do |t|
t.integer "question_id"
t.string "content"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "user_id"
end
Let me know if you have any ideas on how to fix my error of somehow telling rails to look for answers instead of answers_attributes.
UPDATE: Here is my form.
<%= form_for @survey do |f| %>
<%= f.label :user_id %>
<%= f.collection_select :user_id, User.all, :id, :email, {}, { :multiple => false } %><br>
<%= f.text_field :name, placeholder: "Survey name"%>
<%= f.fields_for :questions do |builder| %>
<%= builder.label :content, "Question 1"%>
<%= builder.text_area :content %>
<%= builder.check_box :_destroy %>
<%= builder.label :_destroy, "Remove Question" %>
<%= f.fields_for :answers do |builder| %>
<%= builder.text_field :content, placeholder: "Answer 1" %>
<%= builder.check_box :_destroy %>
<% end %>
<% end %><br>
<%= f.submit "Create", :class => "btn btn-large btn-warning" %>
<% end %>
The solution was an oversight on the forms. I used...
<%= f.fields_for :questions do |builder| %>
...to build the association under the survey and then mistakenly used...
<%= f.fields_for :answers do |builder| %>
...to build the answers under question. I needed to change the f
variable so it shoulder have been something like this:
<%= builder.fields_for :answers do |x| %>
So, it was looking for answers under survey instead of under questions. Thanks guys.