Rails: how to get the name of the previous action in the controller

In rails, I need to check the name of the previous method in the controller. For example: There are methods A, B, C ... etc .; In the controller, I want to execute several statements only if it starts from method A. How to write a condition for this? I saw a lot of links that talked about the name of the current action and the name of the controller, but that was not what I needed. I need to get the name of the previous method. Thanks in advance.

+6
source share
2 answers

You can use request.referer to get the HTTP referral header, which will be the previous visited URL.

However, this is not an ideal way to solve the problem you are facing, as it is unreliable and will break if your URLs change. If you need to save information about your user path through the application, you can try to save the variables in the session:

http://guides.rubyonrails.org/action_controller_overview.html#accessing-the-session

+9
source

You can use this

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

src: fooobar.com/questions/965734 / ...

+3
source

All Articles