0

If I have a route like so

get 'controller/:id/action/:param' => 'Controller#action'

How can I use link_to to create a link like <a href="/controller/12345/action/abcde">?

So far I have gotten link_to 'Link text', {:action => 'action'}

To give me <a href="/controller/12345/action">

but using link_to 'Link text', {:action => 'action', :param => 'abcde'}

will give me <a href="/controller/12345/action?param=abcde">

arcyqwerty
  • 10,325
  • 4
  • 47
  • 84

1 Answers1

1

You can name your route:

get 'controller/:id/action/:param' => 'Controller#action', :as => 'custom_show'

This should give you a new url helper which you can use like this:

<%= link_to "Foobar", custom_show_path(:id=>1234, :param=>'my_param') %>
Wukerplank
  • 4,156
  • 2
  • 28
  • 45
  • I've seen the docs for doing this but I can't seem to get it to work... I'm getting `undefined method 'custom_show_path'` – arcyqwerty Aug 02 '12 at 17:57
  • Does having this route under a resource break this functionality? – arcyqwerty Aug 02 '12 at 17:58
  • Run `rake routes` and have a look at the output. Maybe you can find it there? – Wukerplank Aug 02 '12 at 17:59
  • If it is under a resource it the helper might look different (`myresource_custom_show_path` or `custom_show_myresource_path`). But I'd suggest you move the route outside the resource. – Wukerplank Aug 02 '12 at 18:00