Accessing the controller name in your views?

I need to know what controller and action the user is currently in
to do various things in my layouts/application.rhtml file. Isn’t
there is a better way than:

<% if params[:controller] == “whatever” && params[:action] ==
“whatever” %>some html<% end %>

I know in the controllers you can just use controller_name, but that
is not provided in the views.

Thanks for your help.

Thank You,
Ben J.
E: [email protected]

Why not just assign controller_name to a @variable to use in your views?

Ben J. wrote:

I need to know what controller and action the user is currently in
to do various things in my layouts/application.rhtml file. Isn’t
there is a better way than:

<% if params[:controller] == “whatever” && params[:action] ==
“whatever” %>some html<% end %>

I know in the controllers you can just use controller_name, but that
is not provided in the views.

Thanks for your help.

Thank You,
Ben J.
E: [email protected]

Popular question today.

I have the following in one of my layout head sections

<%= “admin::#{controller.controller_name}::#{controller.action_name}” %>

That might get you started.

Peter

A controller’s purpose in life is to determine what view to render. If
you
find yourself conditionally rendering views within a view, it’s
probably
time to look at your controller again.

  • Rabbit

PS:

But to answer your question, the “proper” way to access controller and
action names would be through params. (e.g. params[:controller])


On Monday, July 24, 2006, at 4:11 PM, Rabbit wrote:

That might get you started.

[email protected]


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

There are plenty of good reasons to want to know what controller you are
in. Particularly if you have some meta magic going on in a helper that
spans multiple controllers.

controller.controller_name and
controller.action_name
are the correct functions to call.

_Kevin
www.sciwerks.com

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs