175

I have a Rails question.

How do I get a controller action's name inside the controller action?

For example, instead of

def create
  logger.info("create")
end

I want to write something like

def create
  logger.info(this_def_name)
end

What is a way to get this_def_name?

7 Answers 7

370

Rails 2.X: @controller.action_name

Rails 3.1.X: controller.action_name, action_name

Rails 3.2+: action_name (still works in Rails 7)

Sign up to request clarification or add additional context in comments.

6 Comments

Or 'controller.action_name' in Rails 3.0. This is the best way to access the action name in the view.
If you want to use this in a before_filter with a block, do before_filter { |controller| ... }. stackoverflow.com/questions/2669663/…
FYI, action_name works in Rails 3.2.13, and controller.action_name doesn't.
rails 2.3.XX and @controller.action_name does not work, you must use just action_name for this version(s).
FYI action_name works in Rails 7.0.x still.
|
37

In the specific case of a Rails action (as opposed to the general case of getting the current method name) you can use params[:action]

Alternatively you might want to look into customising the Rails log format so that the action/method name is included by the format rather than it being in your log message.

4 Comments

You used to be able to get the current action by calling action_name, I'm not sure if that still works, but I always thought it was a bit nicer than querying the params.
Just to clarify, it's "controller.action_name" or "params[:action]", so you might do <% if controller.action_name == 'new' %> in the view. It works for me in Rails 3.2.
You can also get the controller name via params[:controller]
action_name == 'new' (unprefixed) still works for me in Rails 3.2
20

controller name:

<%= controller.controller_name %>

return => 'users'

action name:

<%= controller.action_name %>

return => 'show'

id:

<%= ActionController::Routing::Routes.recognize_path(request.url)[:id] %>

return => '23'

Comments

8

This snippet works for Rails 3

class ReportsController < ApplicationController

  def summary
    logger.debug self.class.to_s + "." + self.action_name
  end

end

will print

. . .
ReportsController.summary
. . .

Comments

3

mikej's answer was very precise and helpful, but the the thing i also wanted to know was how to get current method name in rails.

found out it's possible with self.current_method

easily found at http://www.ruby-forum.com/topic/75258

Comments

-2

You can get the Action Name like this

Controller Method

def abc

   params[:action] #you can store this any variable or you can directly compare them.

   ab == params[:action] 
 
end

This supports from rails 4 to rails 6 versions.

1 Comment

params[:action] this method is already answered by other members. Your codeblock is not clear either, what's ab in there ?
-4

I just did the same. I did it in helper controller, my code is:

def get_controller_name
  controller_name    
end


def get_action_name
  action_name   
end

These methods will return current contoller and action name. Hope it helps

1 Comment

Confusing. Why wrap controller_name, which is a method that returns the name of the current controller, in another method that just calls controller_name? Why not just call controller_name and be done with it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.