Reputation: 3057
I have Request model. I have two types of requests, parent and child. One parent Request can have many child Requests but child can have only one parent.
Suppose I am editing some child Request. After update I want to redirect to its parent Request. Here is my code:
def update
respond_to do |format|
if @request.update(request_params)
format.html { redirect_to request_path(params[:parent_request_id]) , notice: 'Request was successfully updated.' }
format.json { render :show, status: :ok, location: @request }
else
format.html { render :edit }
format.json { render json: @request.errors, status: :unprocessable_entity }
end
end
end
so i redirect to request_path(params[:parent_request_id]). where parent_request_id is defined in hidden_field inside form, and passed through url. Inspecting in browser showed me my hidden field in form with a value set:
<input type="hidden" value="1" name="request[parent_request_id]" id="request_parent_request_id">
But I got error:
No route matches {:action=>"show", :controller=>"requests", :id=>nil} missing required keys: [:id]
Upvotes: 0
Views: 487
Reputation: 2519
You are doing it almost right.
Check the params being sent on the terminal when you submit the form. You will see that all parameters sent to the update action, including [:parent_request_id
], are wrapped in [:request]
.
In order to reach that param, you need to put this:
params[:request][:parent_request_id]
Also, look at the error - it tells you that the value is nil
there, which means that nothing went through. This means that a) You forgot to permit that parameter b) You mispelled it/didn't write the correct value.
Upvotes: 3