# Tell link\_to explicitly what route to use?

**URL:** <https://www.ruby-forum.com/t/tell-link-to-explicitly-what-route-to-use/65671>\
**Category:** Rails\
**Created:** [August 20, 2006, 1:00pm UTC](https://www.ruby-forum.com/t/tell-link-to-explicitly-what-route-to-use/65671 "2006-08-20T13:00:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [August 20, 2006, 1:00pm UTC](https://www.ruby-forum.com/t/tell-link-to-explicitly-what-route-to-use/65671/1 "2006-08-20T13:00:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [August 20, 2006, 2:32pm UTC](https://www.ruby-forum.com/t/tell-link-to-explicitly-what-route-to-use/65671/2 "2006-08-20T14:32:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [August 20, 2006, 2:45pm UTC](https://www.ruby-forum.com/t/tell-link-to-explicitly-what-route-to-use/65671/3 "2006-08-20T14:45:36Z")

</div>

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

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](https://www.ruby-forum.com/music/custom/my_action)

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? 😉

Greetings,  
Josh

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [August 20, 2006, 3:03pm UTC](https://www.ruby-forum.com/t/tell-link-to-explicitly-what-route-to-use/65671/4 "2006-08-20T15:03:06Z")

</div>

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. 😉 The link MUST change, but according to the rules in the  
named route…

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [August 20, 2006, 3:10pm UTC](https://www.ruby-forum.com/t/tell-link-to-explicitly-what-route-to-use/65671/5 "2006-08-20T15:10:43Z")

</div>

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’) %\>

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [August 20, 2006, 3:01pm UTC](https://www.ruby-forum.com/t/tell-link-to-explicitly-what-route-to-use/65671/6 "2006-08-20T15:01:08Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![josh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/45deac/32.png) [@josh](https://www.ruby-forum.com/u/josh)\
**Post date:** [August 20, 2006, 3:12pm UTC](https://www.ruby-forum.com/t/tell-link-to-explicitly-what-route-to-use/65671/7 "2006-08-20T15:12:43Z")

</div>

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!  
😛

Thanks a lot! 🙂
