Tell link_to explicitly what route to use?

Hi all

Is there a way to tell link_to explicitly what route it must use to
construct the link URL? Because I have some very similar looking
routes…

Thanks for info,
Joshua

Sure. Use name routes… so rather than use something like:

map.connect ‘example/:action’, :controller => ‘example’

use:

map.example ‘example/:action’, :controller => ‘example’

this way, you can use example_url(:action => ‘whatever’) to create
the url for that route.

Hope this helps,
Damien

That wasn’t my question, maybe I was not clear enough. :wink:

I know that I can create a specific URL using my_custom_route_url, but
when using link_for I don’t seem to have the option to specify the route
to use. link_to just looks itself for the route that fits…

This sounds quite good, but in my opinion it can be harmful.

Let’s think I have the following routes:

map.my_custom_route ‘music/custom/:action/:id’,
:controller => ‘custom’

map.connect ‘:controller/:action/:id’

Then I want to create a my_custom_route link. I could either use

Some
link

Or I could use

<%= link_to “Some link”, :controller => ‘custom’, :action => ‘my_action’
%>

Both would result in

Some link

So far, so good. But what if I changed the controller of
my_custom_route?

map.my_custom_route ‘music/custom/:action/:id’,
:controller => ‘some_other_controller’

Now the result of the 2 examples above would be different! The
my_custom_route_url would result in
/music/some_other_controller/my_action (what is correct), but the
link_to wouldn’t find the correct route anymore, and it would result in
/some_other_controller/my_action now.

So I’d like to tell the link_to method exactly which route to use.

You know what I mean? :wink:

Greetings,
Josh

harper wrote:

i didn’t really quite understand; although if it helps, why not:

<%= link_to “titleoflink”, “/what/ever/you/want/to/put/here” %>

and if you change anything in your routes rb file, the link will still
work. good enough?

Not really. :wink: The link MUST change, but according to the rules in the
named route…

Sorry if i’m not following, but you can use the named route in the
link_to:

<%= link_to “Some link”, my_custom_route_url(:action => ‘my_action’) %>

i didn’t really quite understand; although if it helps, why not:

<%= link_to “titleoflink”, “/what/ever/you/want/to/put/here” %>

and if you change anything in your routes rb file, the link will still
work. good enough?

Damien wrote:

Sorry if i’m not following, but you can use the named route in the
link_to:

<%= link_to “Some link”, my_custom_route_url(:action => ‘my_action’) %>

Oh yeah, the simplest solutions are always the best… Silly silly me!
:stuck_out_tongue:

Thanks a lot! :slight_smile: