views:

2539

answers:

2

hi, i have a rails question

how do i get controller action name inside the controller action?

f.ex. instead of

def create
logger.info("create")
end

to write something like

def create
logger.info(this_def_name)
end

what is a way to get this_def_name?

+4  A: 

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
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
action_name still works and agree it's a bit nicer :)
mikej
+1  A: 

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.