Forum: Ruby on Rails Request Controller Name in View?

Fd0b8a5e3854d056ee30f07feedf2219?d=identicon&s=25 Mario Gr (mario517)
on 2008-01-31 08:21
Hi,

I was wondering if there were anyway to return the controller name in my
view.  Any help would be appreciated.  Thanks!

-Mario
9a2a53db8e9b4476038c94a64b32833f?d=identicon&s=25 Ryan Bigg (ryan-bigg)
on 2008-01-31 08:45
(Received via mailing list)
params[:controller]

On Jan 31, 2008 5:51 PM, Mario Flores <rails-mailing-list@andreas-s.net>
wrote:

> >
>


--
Ryan Bigg
http://www.frozenplague.net
Feel free to add me to MSN and/or GTalk as this email.
7223c62b7310e164eb79c740188abbda?d=identicon&s=25 Xavier Noria (fxn)
on 2008-01-31 09:23
(Received via mailing list)
On Jan 31, 2008, at 8:21 AM, Mario Flores wrote:

> I was wondering if there were anyway to return the controller name
> in my
> view.  Any help would be appreciated.  Thanks!

Views have a pointer to the controller, but I always setup a couple of
convenience variables in ApplicationController that additionally
follow Rick's authentication plugins notation (@current_user):

    before_filter :set_controller_and_action_names

    def set_controller_and_action_names
      @current_controller = controller_name
      @current_action     = action_name
    end

-- fxn
9a2a53db8e9b4476038c94a64b32833f?d=identicon&s=25 Ryan Bigg (ryan-bigg)
on 2008-01-31 10:23
(Received via mailing list)
Um, why?

params[:controller] and params[:action] do exactly this!

On Jan 31, 2008 6:52 PM, Xavier Noria <fxn@hashref.com> wrote:

>
> >
>


--
Ryan Bigg
http://www.frozenplague.net
Feel free to add me to MSN and/or GTalk as this email.
90db534bf5e6631659b9a159825fb4bd?d=identicon&s=25 envynicky (Guest)
on 2008-01-31 10:40
(Received via mailing list)
you can get the controller name by

 @controller.controller_name
90db534bf5e6631659b9a159825fb4bd?d=identicon&s=25 envynicky (Guest)
on 2008-01-31 10:41
(Received via mailing list)
you can get the controller's name through
 @controller.controller_name in view


On Jan 31, 12:21 pm, Mario Flores <rails-mailing-l...@andreas-s.net>
7223c62b7310e164eb79c740188abbda?d=identicon&s=25 Xavier Noria (fxn)
on 2008-01-31 10:56
(Received via mailing list)
On Jan 31, 2008, at 10:21 AM, Ryan Bigg wrote:

> params[:controller] and params[:action] do exactly this!

If you want to ask for the current controller name in a view the right
path in my opinion is to, ahem, ask the current controller for its name:

   <%= controller.controller_name %>

That's proper use of encapsulation. I just happen to prefer an
instance variable.

-- fxn
Fd0b8a5e3854d056ee30f07feedf2219?d=identicon&s=25 Mario Gr (mario517)
on 2008-01-31 18:12
These actually deliver slightly different results.   My controller is
one of a subest of controllers in an admin directory.

admin/products_controller.rb


params[:controller] returns "admin/products"
controller.controller_name return "products"

One may work better than the other, but it really depends on your need.

Thanks for the help!

-Mario
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.