在@Sasha的帮助下,我创建了一个用于患者治疗的嵌套表格:
现在我收到了这个错误:
UnknownAttributeError in PatientsController#update
unknown attribute: treatment
我的患者更新控制器实际上是这样的:
def update
@patient = Patient.find(params[:id])
respond_to do |format|
if @patient.update_attributes(params[:patient])
format.html { redirect_to @patient, notice: 'Patient was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: "edit" }
format.json { render json: @patient.errors, status: :unprocessable_entity }
end
end
end
这样的形式:
<%= form_for @patient do |f| %>
<%= f.fields_for ([@patient, @patient.treatments.build]) do |tf| %>
<%= render 'treatment_form', form: tf %>
<% end %>
<%= f.fields_for ([@patient, @patient.treatments.build]) do |tf| %>
<%= render 'treatment_form', form: tf %>
<% end %>
<%= f.submit %>
<% end %>
所以我不知道我必须添加到我的患者控制器中?
我改变了我的代码,就像@JimLim推荐的那样,但我得到了同样的错误:
ActiveRecord::UnknownAttributeError in PatientsController#update
unknown attribute: treatment
{"utf8"=>"✓",
"_method"=>"put",
"authenticity_token"=>"OPuS9Mmk3guiV20nkw5OaPUFyjVow49H+MMxY37O0r0=",
"patient"=>{"treatment"=>{"category_id"=>"9",
"content"=>"dsfsdf",
"day"=>"2013-07-21"}},
"commit"=>"Update Patient",
"id"=>"9"}
答案 0 :(得分:2)
参数包括treatment
键,该键不是您的Patient模型中的属性。如果是这种情况,则需要
treatments
而不是treatment
和例如,
<%= f.fields_for :treatments, @patient.treatments.build do |tf| %>
<%= render 'treatment_form', form: tf %>
<% end %>
class Patient
accepts_nested_attributes_for :treatments
end
documentation for fields_for中提供了更详细的说明。