Rails 4 nested form not saving -
i have scoured other questions relating topic here, still having problem. have nested form inside nested form. answer choices inside questions inside survey. had no problem setting questions, , save fine. answer choices, though, not saving, , cannot figure out going wrong.
survey.rb
class survey < activerecord::base belongs_to :author has_many :questions, dependent: :destroy has_many :forms, dependent: :destroy has_many :answer_choices, through: :question validates :name, uniqueness: true accepts_nested_attributes_for :questions, reject_if: proc { |attributes| attributes['text'].blank?}, allow_destroy: true end
question.rb
class question < activerecord::base belongs_to :survey has_many :responses, dependent: :destroy has_many :answer_choices, dependent: :destroy accepts_nested_attributes_for :answer_choices, reject_if: proc { |attributes| attributes['content'].blank?}, allow_destroy: true end
answer_choice.rb
class answerchoice < activerecord::base belongs_to :question end
survey_controller.rb
def new @survey = survey.new(author_id: session[:user_id]) @survey.questions.build @survey.questions.each { |question| 4.times { question.answer_choices.build }} end def edit @survey.questions.build @survey.questions.each { |question| 4.times { question.answer_choices.build }} end def survey_params params.require(:survey).permit(:name, :description, :author_id, questions_attributes: [:id, :text, :required, :response_type, :_destroy, :number]) end
_form.html.erb
<%= form_for(@survey) |f| %> <% if @survey.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@survey.errors.count, "error") %> prohibited survey being saved:</h2> <ul> <% @survey.errors.full_messages.each |message| %> <li><%= message %></li> <% end %> </ul> </div> <% end %> <div class="field"> <%= f.label :name %><br> <%= f.text_field :name %> </div> <div class="field"> <%= f.label :description %><br> <%= f.text_field :description %> </div> <div class="field"> <%= f.hidden_field :author_id %> </div> <h2>questions</h2> <%= f.fields_for(:questions) |ff| %> <%= render 'questions', :f => ff %> <% end %> <div class="actions"> <%= f.submit 'submit question'%> </div> <div class="actions"> <%= link_to 'finish survey', surveys_path %> </div> <% end %>
_questions.html.erb
<div> <span><%= f.index + 1 %>. </span> <div class="field"> <%= f.hidden_field :number, value: f.index + 1 %> </div> <div class="field"> <%= f.label :text, "question" %><br> <%= f.text_field :text %> </div> <div class="field"> <%= f.label :response_type %><br> <%= f.select :response_type, [["yes/no", "yes/no"], ["short answer", "string"], ["long answer", "text"], ["multiple choice", "multi"]] %> </div> <div class="field"> <%= f.label :required %> <%= f.check_box :required %> </div> <div> <p>answer choices</p> <%= f.fields_for(:answer_choices) |ff| %> <%= render 'answer_choices', :f => ff %> <% end %> </div> <div class="field"> <%= f.label :_destroy %> <%= f.check_box :_destroy %> </div> </div>
_answer_choices.html.erb
<div class="field"> <%= f.label :content %><br> <%= f.text_field :content %> </div> <div class="field"> <%= f.label :_destroy %> <%= f.check_box :_destroy %> </div>
a few changes in code.
change new
method
def new @survey = survey.new(author_id: session[:user_id]) @questions = @survey.questions.build @answer_choices = 4.times { @questions.answer_choices.build } end
your survey_params
should this
def survey_params params.require(:survey).permit(:name, :description, :author_id, questions_attributes: [:id, :text, :required, :response_type, :_destroy, :number, answer_choices_attributes: [:id,:content,:_destroy]]) end
and in form
code, change these lines
<%= f.fields_for(:questions) |ff| %> <%= f.fields_for(:answer_choices) |ff| %>
into
<%= f.fields_for @questions |ff| %> <%= f.fields_for @answer_choices |ff| %>
Comments
Post a Comment