简体   繁体   English

更新后如何返回上一页?

[英]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.

相关问题 如何记住上一页中的参数,以便返回到该参数? - How can I remember a parameter from the previous page so I can go back to it? 从我的应用程序注销后,如果按返回按钮,我可以看到上一页 - After Log out from my app, if i press back button, i can see the previous page 更新Rails之后的上一页 - Previous page after updating rails 删除后Rails重定向到上一页(后面) - Rails redirect to previous page (back) after delete 如何返回没有启动应用程序的捆绑安装错误? - how can I go back to an error with bundle install that no start the application? 我可以使浏览器的后退按钮转到编辑页面,而不是创建时的新按钮吗? - Can I make the browser back button go to the edit page instead of new on creation 不允许将CSS携带到上一页(单击浏览器后退按钮后) - Not allowing CSS to be carried to previous page (after clicking browser back button) Rails:登录/注册后重定向回上一页 - Rails: redirect back to previous page after sign in / sign up 在rails 5中单击浏览器的后退按钮后如何避免返回go? - How to avoid to go back after click on back button of browser in rails 5? 当我转到其他页面并返回后退按钮时,仍会显示设计消息 - Devise messages still display when i go to other page and go back through back button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM