Function could determine the caller action?


#1

Is it possible to determine which action call the function ?

In example, func_test in ApplicationController could determine which
action from SiteController is calling her ?

in class ApplicationController I have

def func_test
if ( calling from index )
return “1” end

if ( calling from contact )
return “2” end
end

and in class SiteController

def index
@var = func_test
end

def contact
@var = func_test
end

Thanks


#2

if action_name == ‘index’


Kent


#3

I can’t check this at the moment, but I think Ruby supports this:

def test
caller
end

test # should give the object that called test

http://www.ruby-doc.org/docs/ruby-doc-bundle/Manual/man-1.4/function.html#caller

Although, for your case, action_name is probably more suitable :slight_smile:

-Jonny.


#4

No. action_name is not Ruby or ROR functionnality


#5

Have you tried it?

Kent


#6

Yes just test it, return an error. I need to pass an arguement ?


#7

action_name is an attribute of class AtionController::Base. It does
not require an argument. Can you post the exception stack trace?


Kent


#8

class TestController
def test
render :text => action_name
end
end

Works fine for me.

-Jonny.