I have two tables: Client, Inspection
Inspections are created through a form that returns a CollectionProxy related to a given client like so:
inspections_controller.rb
def create
@inspcetion = @client.inspections.create(inspection_params)
redirect_to client_inspections_path(@client)
end
views/inspections/_form.html
<%= form_for([@client, @client.inspections.build], :html => { :class => 'form' }) do |f| %>
Now I am wondering how to update an existing inspection. I believe that using the same form for my update method, will always create a new object -> .build
Is that right?
The url to the update method seems to call the right inspection ( /clients/cl_id/inspections/insp_id/edit), however the form that is generated appears empty (without any of the information that was included for the creation of the inspection)
<%= link_to 'Edit', edit_client_inspection_path(inspection.client, inspection) %>
I believe that I have to change the form / form_for in the edit view and shouldn't render _form.html.erb, however I am completely stuck on how to accomplish this.
Here is my plain update method from inspections_controller.rb
def update
@inspection = @client.inspection.find(params[:id])
respond_to do |format|
if @inspection.update(inspection_params)
format.html { redirect_to @inspection, notice: 'Inspection was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @client.inspection.errors, status: :unprocessable_entity }
end
end
end
So, how can I update an object that is part of a CollectionProxy? It can't be that hard :)
Hope that my question is concise and that somebody can help me out here.