Request Controller Name in View?


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

-- fxn

Um, why?

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

you can get the controller name by


you can get the controller's name through
@controller.controller_name in view

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

These actually deliver slightly different results. My controller is
one of a subest of controllers in an admin directory.


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!