如何在 Ruby On Rails 中重定向到上一页? [英] How to redirect to previous page in Ruby On Rails?

查看:41
本文介绍了如何在 Ruby On Rails 中重定向到上一页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面列出了所有具有可排序标题和分页的项目.

I have a page that lists all of the projects that has sortable headers and pagination.

path:
/projects?order=asc&page=3&sort=code

我选择编辑其中一个项目

I choose to edit one of the projects

path:
projects/436/edit

当我点击该页面上的保存时,它会调用项目控制器/更新方法.更新代码后,我想重定向到单击编辑特定项目之前所在的路径.换句话说,我想以相同的排序在同一页面上.

When I click save on that page, it calls the projects controller / update method. After I update the code I want to redirect to the path that I was on before I clicked edit a specific project. In other words, I want to be on the same page with the same sorting.

我看到了 link_to(:back) 并认为 :back 可以在 redirect_to(:back) 中工作,但这是行不通的.

I saw link_to(:back) and thought that :back may work in redirect_to(:back), but that's a no go.

puts YAML::dump(:back) 
yields the following:
:back 

关于如何让它发挥作用的任何想法.这似乎是一个很容易解决的问题,但我是 RoR 的新手.

Any ideas on How I could get this to work. It seems like a problem that would be easily solved, but I'm new to RoR.

推荐答案

在您的编辑操作中,将请求 url 存储在会话哈希中,该哈希可跨多个请求使用:

In your edit action, store the requesting url in the session hash, which is available across multiple requests:

session[:return_to] ||= request.referer

然后在您的更新操作中重定向到它,成功保存后:

Then redirect to it in your update action, after a successful save:

redirect_to session.delete(:return_to)

这篇关于如何在 Ruby On Rails 中重定向到上一页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆