# \[newbie\]how to use link\_to to link to another controller?

**URL:** <https://www.ruby-forum.com/t/newbie-how-to-use-link-to-to-link-to-another-controller/63598>\
**Category:** Rails\
**Created:** [January 21, 2006, 1:23am UTC](https://www.ruby-forum.com/t/newbie-how-to-use-link-to-to-link-to-another-controller/63598 "2006-01-21T01:23:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 21, 2006, 1:23am UTC](https://www.ruby-forum.com/t/newbie-how-to-use-link-to-to-link-to-another-controller/63598/1 "2006-01-21T01:23:56Z")

</div>

I am just diving into Rails and I am a bit puzzled by the link\_to  
function.

am writing an app to display my photographs in categories. My  
application layout  
has a sidebar to show a menu with categories from the categories table  
in the  
database (like in taken from the tutorials), to be shown on every page.

I managed to get a global @menu\_categories object that keeps track of  
all the  
categories that should pop up in the menu using in the application  
controller  
def initialize  
@menu\_categories=Category.find\_all  
end

Now, if I iterate over these menu categories in the application template  
\<% for menu\_category in @menu\_categories %\>  
\<% link\_to menu\_category.category, :action =\>‘list\_photos’,:id  
=\>menu\_category%\>  
  
\<% end %\>

I get the following links:  
[http://192.168.1.100:8000/categories/list\_photos/3](http://192.168.1.100:8000/categories/list_photos/3) (when in categories  
conroller)  
[http://192.168.1.100:8000/photos/list\_photos/3](http://192.168.1.100:8000/photos/list_photos/3) (when in photos  
controller)

What I want however is to have the second link always, regardles where I  
am in  
the app. So How do I instruct the link\_to helper to always link to a  
specific  
controller in the app?

Cheers VidJa

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 21, 2006, 1:32am UTC](https://www.ruby-forum.com/t/newbie-how-to-use-link-to-to-link-to-another-controller/63598/2 "2006-01-21T01:32:59Z")

</div>

\<%= link\_to :controller =\> “category”, :action =\> “list\_photos”, :id =\>  
IDHERE %\>

~Ben

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 21, 2006, 1:36am UTC](https://www.ruby-forum.com/t/newbie-how-to-use-link-to-to-link-to-another-controller/63598/3 "2006-01-21T01:36:00Z")

</div>

Possibly:  
\<% link\_to menu\_category.category, {:controller =\> ‘your\_controller’,  
:action =\>‘list\_photos’, :id=\>menu\_category}%\>

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 21, 2006, 11:12am UTC](https://www.ruby-forum.com/t/newbie-how-to-use-link-to-to-link-to-another-controller/63598/4 "2006-01-21T11:12:22Z")

</div>

Thanks Ben and Dylan,

it works. I have been looking at the API docs before, but actually I  
can’t find the info for the controller option.  
After some digging this morning I found that the link in the link\_to  
section probably should lead to  
[http://api.rubyonrails.com/classes/ActionController/Base.html#M000174](http://api.rubyonrails.com/classes/ActionController/Base.html#M000174)  
instead of  
[http://api.rubyonrails.com/classes/ActionView/Helpers/UrlHelper.html#M000304](http://api.rubyonrails.com/classes/ActionView/Helpers/UrlHelper.html#M000304)  
since that is where the options are eplained.  
Maybe that was the reason I couldn’t find it?

Anyway, many thanks. Ruby aint bad as a the second language after php to  
learn

Ben R. wrote:

> Dylan has it … forgot to include the “title” field…
> 
> Be sure to use the API Docs. They are very helpful:
> 
> [Peak Obsession](http://api.rubyonrails.com/classes/ActionView/Helpers/UrlHelper.html#M000332)\<  
> link\_to info…

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [January 21, 2006, 1:39am UTC](https://www.ruby-forum.com/t/newbie-how-to-use-link-to-to-link-to-another-controller/63598/5 "2006-01-21T01:39:01Z")

</div>

Dylan has it … forgot to include the “title” field…

Be sure to use the API Docs. They are very helpful:

[Peak Obsession](http://api.rubyonrails.com/classes/ActionView/Helpers/UrlHelper.html#M000332)\<  
link\_to info…
