Forum: Ruby on Rails [newbie]how to use link_to to link to another controller?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
unknown (Guest)
on 2006-01-21 02:23
(Received via mailing list)
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%></a><br />
<% end %>

I get the following links:
http://192.168.1.100:8000/categories/list_photos/3 (when in categories
conroller)
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
Ben R. (Guest)
on 2006-01-21 02:32
(Received via mailing list)
<%= link_to :controller => "category", :action => "list_photos", :id =>
IDHERE %>

~Ben
Dylan S. (Guest)
on 2006-01-21 02:36
(Received via mailing list)
Possibly:
<% link_to menu_category.category, {:controller => 'your_controller',
:action =>'list_photos', :id=>menu_category}%>
Ben R. (Guest)
on 2006-01-21 02:39
(Received via mailing list)
Dylan has it ... forgot to include the "title" field...

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

http://api.rubyonrails.com/classes/ActionView/Help...
link_to info...
VidJa (Guest)
on 2006-01-21 12:12
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/ActionControlle...
instead of
http://api.rubyonrails.com/classes/ActionView/Help...
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:
>
> http://api.rubyonrails.com/classes/ActionView/Help...
> link_to info...
This topic is locked and can not be replied to.