[英]Rails Strong Params & Nested Forms: “Unpermitted parameter: answer”
我有一個評估模型,其中包含許多問題和通過問題提供的許多答案
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
我想創建一個可以接受嵌套屬性作為答案的評估表。
這是我的評估表:
<%= 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 %>
而我的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>
最后我的行動:
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
提交表單時,我進入了Assessments_path,但是我的日志顯示“未經允許的參數答案”。 我知道我的多元化或關聯性出了問題,但不確定確切的位置。
accepts_nested_attributes_for :answers
,看來您是accepts_nested_attributes_for :answers
在錯誤的位置,而您卻缺少questions
accepts_nested_attributes_for :answers
。
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.