2013-07-21 17 views
0

隨着@Sasha的幫助下,我在患者創造了一個嵌套形式處理:UnknownAttributeError在控制器

現在我得到這個錯誤:

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 recomended,但我得到了同樣的錯誤:

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

您的患者模型是什麼樣的? – pdoherty926

+0

attr_accessible:改變,:nachnahme,:ORT,:大街,:電話,:vorname,:treatment_attributes,:治療 的has_many:治療 的has_many:類別,:通過=>:治療 的has_many:畫,:通過=>:治療 accep_nested_attributes_for:治療 –

+2

再次。 「治療」不應該attr_accessible。這不是病人的特徵;這完全是另一種模式。您有治療屬性(可能需要更改爲treatments_attributes,不確定),因爲您說您接受*屬性*進行治療,但治療本身不是患者模型的一部分。如果你刪除它,它可能會修復一些問題。無論如何,它可能應該被刪除。 – Sasha

回答

2

的參數包括treatment關鍵,是不是在你的患者模型的屬性。如果是這樣的話,你需要

  1. 變化等形式,關鍵是treatments而不是treatment
  2. 在你的病人模型添加#accepts_nested_attribute_for

例如,

<%= 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可用。

+0

好吧,我更新了我的問題!謝謝你的幫助! –

+0

謝謝。我的猜測是對的,答案仍然適用。請接受我的答案,如果它適合你。 –