How do I get the current action from within a controller? current_page?
works for views, but not controllers.
redirect_to step3_users_path unless current_page?(step3_users_path)
I also tried
controller.action_name == 'step3'
I also tried
params[:action_name] == 'step3'
If you require to get current controller name in your view file or in your middleware or your serviceprovider etc. you can get your controller details from current route like UserController, HomeController ect. you can also get full path of controller file.
To return a view from the controller action method, we can use View() method by passing respective parameters. return View(“ViewName”) – returns the view name specified in the current view folder (view extension name “. cshtml” is not required. return View("~/Views/Account/Register.
An action (or action method ) is a method on a controller that handles incoming requests. Controllers provide a logical means of grouping similar actions together, allowing common sets of rules (e.g. routing, caching, authorization) to be applied collectively. Incoming requests are mapped to actions through routing.
Controllers have action_name
method/accessor (defined in AbstractController::Base
for current rails, ActionController::Base
for older)
you can use __method__
to get to the name of the current method.
e.g
def Klass
def method1
puts __method__
end
end
> k = Klass.new
> k.method1
=> :method1
Once the request goes through Rails, you will be able to access this in the Controller:
params[:controller]
params[:action]
In a view, you can access action_name
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With