ruby on rails - UnknownAttributeError in Controller -
with of @sasha created nested form treatments in patients:
now error:
unknownattributeerror in patientscontroller#update unknown attribute: treatment
my patients update controller looks this:
def update @patient = patient.find(params[:id]) respond_to |format| if @patient.update_attributes(params[:patient]) format.html { redirect_to @patient, notice: 'patient updated.' } format.json { head :no_content } else format.html { render action: "edit" } format.json { render json: @patient.errors, status: :unprocessable_entity } end end end
and form this:
<%= form_for @patient |f| %> <%= f.fields_for ([@patient, @patient.treatments.build]) |tf| %> <%= render 'treatment_form', form: tf %> <% end %> <%= f.fields_for ([@patient, @patient.treatments.build]) |tf| %> <%= render 'treatment_form', form: tf %> <% end %> <%= f.submit %> <% end %>
so have no clue have add patients controller?
i changed code @jimlim recomended, same error:
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"}
the parameters include treatment
key not attribute in patient model. if case, need to
- change form such key
treatments
instead oftreatment
, and - add #accepts_nested_attribute_for in patient model.
for example,
<%= f.fields_for :treatments, @patient.treatments.build |tf| %> <%= render 'treatment_form', form: tf %> <% end %> class patient accepts_nested_attributes_for :treatments end
a more detailed explanation available in documentation fields_for.
Comments
Post a Comment