[英]How can I go back to the previous page after updating?
How can I go back to the previous page (where the items in question were listed) from the edit view (edit.html.erb) after clicking on the update button? 单击更新按钮后,如何从编辑视图(edit.html.erb)返回上一页(列出有问题的项目)? I have tried: 我努力了:
redirect_to request.referer #stays on the same page(edit.html.erb)
redirect_to session.delete(:return_to) #outputs "Cannot redirect to nil!"
redirect_to :back #stays on same page
redirect_back fallback_location: posts_path #stays on same page
redirect_to(request.env['HTTP_REFERER']) #stays on same page
In the database, the comment is updated. 在数据库中,注释已更新。
While I can use posts_path(@comment), I want to also edit comment from another url. 虽然我可以使用posts_path(@comment),但我也想编辑另一个URL的注释。 Hence the need for a redirect to the previous page. 因此,需要重定向到上一页。
You can't call request.referer
to directly back the the previous of edit page. 您无法调用request.referer
直接返回上一个编辑页面。 Cause on the update
method, the request.referer
will be always the edit
path. 由于update
方法的原因, request.referer
将始终是edit
路径。
You can, do a trick like, parse the request.referer
of edit
action (might be the index
one, for ex. as params to update
action, and redirect to the path on updating the resource ) 您可以执行一些技巧,例如解析request.referer
edit
操作(可能是index
,例如作为update
操作的参数,并重定向到更新资源的路径)
# edit form view
<%= hidden_field_tag :previous_request, request.referer %>
# update action
def update
...
redirect_to params[:previous_request]
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.