我有Request模型。我有两种类型的请求,父母和孩子。一个父请求可以有多个子请求,但子只能有一个父请求。
假设我正在编辑一些子请求。更新后,我想重定向到其父请求。 这是我的代码:
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
所以我重定向到request_path(params [:parent_request_id])。 其中parent_request_id在表单内的hidden_field中定义,并通过url传递。 在浏览器中检查向我显示了我的隐藏字段,其中包含值集:
<input type="hidden" value="1" name="request[parent_request_id]" id="request_parent_request_id">
但我收到了错误:
No route matches {:action=>"show", :controller=>"requests", :id=>nil} missing required keys: [:id]
答案 0 :(得分:3)
你这样做差不多。
检查提交表单时在终端上发送的参数。您会看到发送到更新操作的所有参数(包括[:parent_request_id
]都包含在[:request]
中。
为了达到那个参数,你需要把它放在:
params[:request][:parent_request_id]
另外,看看错误 - 它告诉你那里的值是nil
,这意味着没有任何事情发生。这意味着a)您忘记允许该参数b)您拼错它/没有写出正确的值。