Request Controller Name in View?

Hi,

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

-Mario

params[:controller]

On Jan 31, 2008 5:51 PM, Mario F. [email protected]
wrote:


Ryan B.

Feel free to add me to MSN and/or GTalk as this email.

Um, why?

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

On Jan 31, 2008 6:52 PM, Xavier N. [email protected] wrote:


Ryan B.

Feel free to add me to MSN and/or GTalk as this email.

you can get the controller name by

@controller.controller_name

On Jan 31, 2008, at 8:21 AM, Mario F. 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

On Jan 31, 2008, at 10:21 AM, Ryan B. 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

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

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

On Jan 31, 12:21 pm, Mario F. [email protected]