Rails 3 - retrieving last accessed action/controller from current controller

匿名 (未验证) 提交于 2019-12-03 01:40:02

问题:

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

e.g

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

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

I am using devise in my app if that helps.

Is there another method ?

Thanks Alex

回答1:

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.

e.g.

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

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


回答2:

This question was asked a long time ago, but for anyone else viewing this, my solution (though a bit of a hack) was:

url = Rails.application.routes.recognize_path(request.referrer) last_controller = url[:controller] last_action = url[:action]


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!