Get current controller name/action name from view

Hi.

I swear I found this in the group archives, but now I cannot find this
in the group or elsewhere online!

In a view I would like to get the current controller name and action
that was used to get here.

For example, I have a template that I’m using from two different
controllers (and four different actions in each controller), such
as :controller => “my_view”, :action => “current” (or “expired”, or
“fixed”, etc.)

In this template I’m constructing some links to specific parameter
calls of this same template, and I need to know which controller was
used and what action was called to get us here. I thought it was
something like this:

 link_to "This Year", :controller => @controller.name, :action =>
    @controller.action

The only thing I’ve found in the api that looks remotely similar is
UrlHelper’s current_page(), but that’s not quite what I’m after, as I
don’t want to test something, I just WANT that something :slight_smile:

Thanks.

Here is a link to what I think you are looking for

http://www.postal-code.com/mrhappy/blog/2007/01/18/rails-knowing-the-current-action-in-a-view/

Good luck.

In a view I would like to get the current controller name and action
that was used to get here.
params[:controller] params[:action]
In this template I’m constructing some links to specific parameter
calls of this same template, and I need to know which controller was
used and what action was called to get us here. I thought it was
something like this:

link_to “This Year”, :controller => @controller.name, :action =>
@controller.action

Use url_for (or a named route), then don’t pass the controller in.
Url_for
will find the controller in params if you don’t pass it in.

(Write tests for all this stuff, using assert_xpath, to detect you are
getting
the links you expect!)

Excellent, thanks!

I also just found this:
http://snippets.dzone.com/posts/show/4391

<%= controller.controller_name %>
<%= controller.action_name %>
1 Like