# Calling a self controller path on method from view

**URL:** <https://www.ruby-forum.com/t/calling-a-self-controller-path-on-method-from-view/170330>\
**Category:** Rails\
**Created:** [June 16, 2009, 3:02am UTC](https://www.ruby-forum.com/t/calling-a-self-controller-path-on-method-from-view/170330 "2009-06-16T03:02:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [June 16, 2009, 3:02am UTC](https://www.ruby-forum.com/t/calling-a-self-controller-path-on-method-from-view/170330/1 "2009-06-16T03:02:17Z")

</div>

I’ve created a helper for dealing with tables called TableHelper. I  
have a method to deal with sorting asc or desc from within columns.  
They involve images and some other transitions. Here’s the code:

def sort\_column(title, direction)  
(link\_to image\_tag(“up.gif”, :border=\>0),  
rushing\_offenses\_path(:numteams =\> (@showall ? 120 : nil), :orderby =\>  
title, :sortby =\> direction)) if !@searchteams  
end

However, I’m trying to keep the code dry and I want to reuse the same  
method for all of the models/views that require it. Unfortunately, it  
has a default path pointing to the current controller which happens to  
be rushing\_offenses\_path.

I want to call/change this path based on the controller that calls it.  
Unfortunately, I searched forever, and watched many rails casts to see  
if I could find the information. While I learned a ton of new things  
with helpers today, I couldn’t find any information on what I’m trying  
to do.

Many thanks in advance.

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [June 16, 2009, 4:05am UTC](https://www.ruby-forum.com/t/calling-a-self-controller-path-on-method-from-view/170330/2 "2009-06-16T04:05:50Z")

</div>

On Tue, 2009-06-16 at 03:02 +0200, Ã„lphÃ¤ BlÃ¼Ã« wrote:

> I want to call/change this path based on the controller that calls it.

Not sure if this is what you seek, but all views have access to the  
controller / action that rendered them via controller.controller\_name  
and controller.action\_name.  
e.g.;

\<% if controller.controller\_name == “rushing\_offenses” -%\>  
some stuff  
\<% end %\>

HTH,  
Bill

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [June 16, 2009, 5:05am UTC](https://www.ruby-forum.com/t/calling-a-self-controller-path-on-method-from-view/170330/3 "2009-06-16T05:05:26Z")

</div>

You can use that technique to do something like this in your helper:

link\_to(“Link!”, { :action =\> “blah”, :controller =\>  
controller.controller\_name })

You don’t have to use named routes all the time. There are definitely  
times where the path hash is advantageous.

---

<div class="post-metadata">

**Author:** ![elricstorm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/da6949/32.png) [@elricstorm](https://www.ruby-forum.com/u/elricstorm)\
**Post date:** [June 16, 2009, 5:52pm UTC](https://www.ruby-forum.com/t/calling-a-self-controller-path-on-method-from-view/170330/4 "2009-06-16T17:52:06Z")

</div>

I created the following for my TableHelper which will allow all 37  
models to use the same thing:

def sort\_column(title, direction)  
(direction == “asc” ? image = “up.gif” : image = “down.gif”)  
(link\_to image\_tag(image, :border=\>0), :controller =\>  
controller.controller\_name, :numteams =\> (@showall ? 120 : nil),  
:orderby =\> title, :sortby =\> direction) if !@searchteams

end
