0

I have an Assessment model which has many questions and many answers through questions

class Assessment < ActiveRecord::Base
  belongs_to :template
  belongs_to :patient
  has_many :questions, :through=> :template
  has_many :answers, :through=> :questions
  accepts_nested_attributes_for :answers
end

class Question < ActiveRecord::Base
  belongs_to :template
  has_many :answers
end

class Answer < ActiveRecord::Base
  belongs_to :question
  belongs_to :assessment
end

I want to create an assessment form which can accept nested attributes for answers.

Here's my assessment form:

<%= form_for @assessment do |f| %>

    <div role="tabpanel" class="tab-pane active" id="subjective">
      <% @assessment.questions.each do |question| %>
        <%= render 'question_fields', :question=> question, :f=> f %>
      <% end %>
    </div>

  <%= f.hidden_field :patient_id, :value=> @assessment.patient.id %>
  <%= f.hidden_field :template_id, :value=> @assessment.id %>
  <%= f.submit 'Save', :class=> "btn btn-primary", :style=>"width: 100%;" %>

<% end %>

And my question_fields:

<p><%= question.content %></p>

<%= f.fields_for :answer do |builder| %>

  <div class="row">
    <div class="col-sm-10 question">
      <% if question.field_type == "text_area" %>
        <%= builder.text_area :content, :class=>"form-control" %>
      <% end %>
    </div>
  </div>

And finally my action:

def create
    @assessment = current_user.assessments.new(assessment_params)

    if @assessment.save
      redirect_to assessments_path
    else
      render :new
    end
  end


protected

   def assessment_params
      params.require(:assessment).permit(
        :template_id, :patient_id,
        :answers_attributes => [:id, :question_id, :assessment_id, :tracking,    :content])
   end

When I submit my form, I get taken to the assessments_path, but my log shows "unpermitted params answer". I know somewhere I've gone wrong with my pluralization or associations, but not sure exactly where.

Jackson Cunningham
  • 4,283
  • 3
  • 27
  • 64

1 Answers1

0

Give this a shot, it looks like you're accepts_nested_attributes_for :answers is in the wrong place and you're missing the one for questions.

class Assessment < ActiveRecord::Base
  belongs_to :template
  belongs_to :patient
  has_many :questions, :through=> :template
  has_many :answers, :through=> :questions
  accepts_nested_attributes_for :questions
end

class Question < ActiveRecord::Base
  belongs_to :template
  has_many :answers
  accepts_nested_attributes_for :answers
end

class Answer < ActiveRecord::Base
  belongs_to :question
  belongs_to :assessment
end
kobaltz
  • 6,433
  • 1
  • 31
  • 50
  • How would I build the form in this case? Because I currently am not submitting any fields for the question. – Jackson Cunningham Jun 08 '15 at 20:25
  • worst case, you could create a `attr_accessor :dummy` virtual variable on the questions and pass a random string. This would create the `question_attributes` key and allow for the nested `answer_attributes` on the questions. – kobaltz Jun 08 '15 at 20:29