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
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.
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
Or I could use
<%= link_to “Some link”, :controller => ‘custom’, :action => ‘my_action’
%>
Both would result in
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
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…
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!
Thanks a lot!
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs