Getting the name of the calling controller


#1

I know I’ve seen this before, but I can’t find it and Google hasn’t
been a help…

From within a controller method, how can I find the name of the
requesting controller? For example, if I’m in controller C1 and I do
a
redirect_to :controller=> ‘foo’, :action => ‘bar’,
how can I find the name of the requesting controller (C1) from within
method ‘bar’?

I thought it was request.controller, but that doesn’t seem to work.

Regards

Dave M.


#2

Try ActionController::Base#controller_name

Kent.


#3

Why not just pass it on query string. Redirect_to is a full 302 browser
redirect, so it shouldn’t have any knowledge (outside of HTTP_REFERER,
which
isn’t dependable) of the previous controller. However, as with
everything
else in life, I could be wrong.

Bob S.
http://www.railtie.net/


#4

Hmm, doesn’t seem to exist after all.

Best I can come up with is
request.path.split(’/’)[1]
which isn’t exactly intuitive and may not work in some environments…

Regards

Dave M.


#5

params[:controller] ??


#6

David M. wrote:

Hmm, doesn’t seem to exist after all.

Best I can come up with is
request.path.split(’/’)[1]
which isn’t exactly intuitive and may not work in some environments…
Without specific support, I’d put this in the application controller:

after_filter {session[:last_params] = params}

That way you can get the last controller name as
session[:last_params][:controller] from anywhere.


#7

Franck wrote:

params[:controller] ??
Um… Yes?


Alex