Rails 3-从当前控制器中检索上次访问的操作/控制器 [英] Rails 3 - retrieving last accessed action/controller from current controller

查看:79
本文介绍了Rails 3-从当前控制器中检索上次访问的操作/控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要能够根据用户先前所在的页面在当前页面上设置导航。

I need to be able to set my nav on the current page depending on which page the user was perviously on.

例如

pageX => pageA(tab1selected)
pageY => pageA(tab2selected)

pageX => pageA(tab1selected) pageY => pageA(tab2selected)

我从阅读中知道可以使用request.env [ HTTP_REFERER],但我读到,如果用户有防火墙等,并不能总得到回报。

I know from reading you can use request.env["HTTP_REFERER"] but I read this doesnt always get return if the user has a firewall etc

如果有帮助,我正在我的应用程序中使用devise。

I am using devise in my app if that helps.

还有另一种方法吗?

谢谢
Alex

Thanks Alex

推荐答案

虽然不是一种快速解决方案,但它的工作原理是:

Though not a quick solution, it works:

最后,每个控制器带有视图的 ,请调用一种方法来设置您在会话中存储当前操作。如果您使用多个控制器,则为该控制器创建另一个变量。

At the end each controller with view, call a method to set your store your current action in the session. If you use several controllers, then create another variable for the controller.

例如

def index
  ... # your stuff
  set_action("index")
end

protected
def set_action(action_name)
  session[:action]=action_name
  #session[:controller]="my_controller_name"
end

您可以在每个控制器中重新创建set_action方法,或者创建一个帮助器,然后使用2个参数:

You can recreate the set_action method in each controller, or make a helper and then use 2 arguments:

def last_visited(action_name, controller_name)
  session[:action]=action_name
  session[:controller]=controller_name
end

这篇关于Rails 3-从当前控制器中检索上次访问的操作/控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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