0

I'm trying nested objects in a Rails4-app according to this Railscast. The model survey has_many :questions, the model questions in turn has_many :answers, the model answers belongs_to :questions and questions belongs_to :survey.

Now, the models were at first completely separated from one another which worked fine although I did not want it that way. I preferred them to be nested into one another so that I could assign and display the different objects at the same time.

I then had to figure out how to white-list strong parameters of these nested objects/attributes and there were good questions here that helped me with that.

When I create my database entries everything works fine. The problem comes when I want to edit the object in the database. In the log I get "Unpermitted parameter: answers" even though I've whitelisted every attribute including the ones for answers. I simply don't understand why.

Anybody who can point me in the right direction?

My surveys_controller:

class SurveysController < ApplicationController
 before_action :set_survey, only: [:show, :edit, :update, :destroy]
  def index
    @surveys = Survey.all
  end

  def show
    @survey = Survey.find(params[:id])
  end

  def new
    @survey = Survey.new
    3.times do 
      question = @survey.questions.build
      4.times { question.answers.build }
    end
  end

  def create
    @survey = Survey.new(survey_params)
    if @survey.save
      flash[:notice] = 'Survey was successfully created.' 
      redirect_to(:action => 'index')
    else
      render('new') 
    end
  end

  def edit
    @survey = Survey.find(params[:id])
  end

  def update
    #Find an existing object using form parameters
    @survey = Survey.find(params[:id])
    #Update the object
    if @survey.update_attributes(survey_params)
      flash[:notice] = "Survey updated successfully."
      #If update succeeds, redirect to 'show' action.
      redirect_to(:action => 'show', :id => @survey.id)
    else
      #Else redisplay the 'edit' form.
      render('edit')
    end
  end 

  def delete
    @survey = Survey.find(params[:id])
  end

  def destroy
     @survey = Survey.find(params[:id]).destroy
     flash[:notice] = "Survey destroyed successfully."
     redirect_to(:action => 'index')
  end

  private

   def set_survey
      @survey = Survey.find(params[:id])
   end

  def survey_params
         params.require(:survey).permit(:name, questions_attributes: [:survey_id, :id, :content, answers_attributes: [:id, :question_id, :correct_answer, :content]])
  end
end

My survey.rb model:

class Survey < ActiveRecord::Base
     has_many :questions, :dependent => :destroy
     accepts_nested_attributes_for :questions, :reject_if => lambda { |a| a[:content].blank? }

     scope :sorted, lambda { order("questions.created_at DESC")}
end

EDIT: My question.rb-model:

class Question < ActiveRecord::Base
    belongs_to :survey
    has_many :answers, :dependent => :destroy
    accepts_nested_attributes_for :answers, :reject_if => lambda { |a| a[:content].blank? }

    scope :sorted, lambda { order("questions.created_at DESC")}
end

my answer.rb-model

class Answer < ActiveRecord::Base
   belongs_to :question
end

my /surveys/show.html.erb

<td><%= link_to('<< Back to list', {:action => 'index'}, :class =>     'action_index') %></td>

<div class="survey show">
  <h2><strong>Show survey:</strong></h2>
    <table summary="Survey detail view">
        <tr>
           <th>Survey name: </th>
             <td><%= h @survey.name %></td>
        </tr>
            <tr>
               <th>Question: </th>
                   <td><% for question in @survey.questions do %>
                        <li><%= h question.content %></li>
                        <ul>
                            <% for answer in question.answers do %>
                                <li><%= h answer.content %></li>
                            <% end %>
                        </ul>
                      <% end %>
                   </td>
         </tr>

    <tr>
        <th>Created_at: </th>
            <td><%= @survey.created_at %></td>
    </tr>

  <tr>

    <td><%= link_to('Edit', {:action => 'edit', :id => @survey.id }, :class => 'action_edit')  %></td>
    <td><%= link_to('Delete', {:action => 'destroy', :id => @survey.id }, :class => 'action_edit')  %></td>
      </tr>
    </table>
</div>

My _form_for.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 |ff| %>
          <%= render 'question_fields', :f => ff %>
       <% end %>
       <%= f.fields_for :answers do |fff| %>
            <%= render 'answer_fields', :f => fff %>
       <% end %>
    <p><%= f.submit "Submit" %></p>
 <% end %>

My _question_field.html.erb

<p>
   <%= f.label :content, "Question" %><br />
   <%= f.text_area :content, :rows => 3 %><br />
</p>

My _answer_field.html.erb

<p>
   <%= f.label :content, "Answer" %>
   <%= f.text_field :content %>
   <%= f.radio_button :correct_answer, true %>
</p>
Community
  • 1
  • 1
ljnissen
  • 139
  • 1
  • 12

1 Answers1

1

You have posted your Survey model twice instead of your Question model. Does your question model accept_nested_attributes_for :answers?

Assuming that you have and that I understand your structure correctly, your problem is most likely in your form - instead of f.fields_for :answers, you should have ff.fields_for :answers, nested within f.fields_for :questions, as this is a nested resource of Question and not Survey. So:

<%= f.fields_for :questions do |ff| %>
  <%= render 'question_fields', :f => ff %>

  <%= ff.fields_for :answers do |fff| %>
    <%= render 'answer_fields', :f => fff %>
  <% end %>

<% end %>
shannondoah
  • 90
  • 1
  • 7
  • This was it. Thank you very much! I've also updated my question so that it reflects your comments about my models. – ljnissen Apr 15 '15 at 10:40