172

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?

februaryInk
  • 705
  • 1
  • 8
  • 15
Pavel K.
  • 6,697
  • 8
  • 49
  • 80

7 Answers7

364

Rails 2.X: @controller.action_name

Rails 3.1.X: controller.action_name, action_name

Rails 4+: action_name

JellicleCat
  • 28,480
  • 24
  • 109
  • 162
36

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.

mikej
  • 65,295
  • 17
  • 152
  • 131
  • 8
    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. – jonnii Aug 07 '09 at 14:46
  • 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. – dmonopoly Mar 16 '12 at 10:43
  • 1
    You can also get the controller name via params[:controller] – Ash Blue Mar 18 '13 at 20:04
  • `action_name == 'new'` (unprefixed) still works for me in Rails 3.2 – Brendon Muir Apr 06 '13 at 00:57
19

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'

dumP
  • 771
  • 8
  • 17
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
. . .

Vladimir Kroz
  • 5,237
  • 6
  • 39
  • 50
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

Pavel K.
  • 6,697
  • 8
  • 49
  • 80
-1

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.

Imam
  • 997
  • 7
  • 13
  • 1
    `params[:action]` this method is already answered by other members. Your codeblock is not clear either, what's `ab` in there ? – Imam May 31 '21 at 05:12
-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

mikeycgto
  • 3,368
  • 3
  • 36
  • 47
  • 40
    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? – piersadrian Jul 21 '12 at 23:32