开发者

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

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开发者_如何学C there another method ?

Thanks Alex


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]


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜