UnknownAttributeError in the controller

With @Sasha, I created a nested form for treating patients:

Now I get this error:

UnknownAttributeError in PatientsController#update unknown attribute: treatment 

My update controller for my patients looks something like this:

 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 

And a form like this:

 <%= 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 %> 

So, I have no idea what I should add to my patients' controller?

I changed my code as @JimLim recommended, but I get the 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"} 
0
source share
1 answer

Options include a treatment key, which is not an attribute in your patient model. If so, you need to

For instance,

 <%= f.fields_for :treatments, @patient.treatments.build do |tf| %> <%= render 'treatment_form', form: tf %> <% end %> class Patient accepts_nested_attributes_for :treatments end 

A more detailed explanation is available in the documentation for fields_for .

+2
source

Source: https://habr.com/ru/post/1397819/


All Articles