Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I prevent link_to from escaping slashes in URL parameters in Rails?

Having this route:

map.foo 'foo/*path', :controller => 'foo', :action => 'index'

I have the following results for the link_to call

link_to "Foo", :controller => 'foo', :path => 'bar/baz'
# <a href="/foo/bar%2Fbaz">Foo</a>

Calling url_for or foo_url directly, even with :escape => false, give me the same url:

foo_url(:path => 'bar/baz', :escape => false, :only_path => true)
# /foo/bar%2Fbaz

I want the resulting url to be: /foo/bar/baz

Is there a way around this without patching rails?

like image 686
kch Avatar asked Nov 14 '22 17:11

kch


1 Answers

Instead of passing path a string, give it an array.

link_to "Foo", :controller => 'foo', :path => %w(bar baz)
# <a href="/foo/bar/baz">Foo</a>

If you didn't have the route in your routes file, this same link_to would instead create this:

# <a href="/foo?path[]=bar&path[]=baz">Foo</a>

The only place I could find this documented is in this ticket.

like image 195
Gordon Wilson Avatar answered Dec 20 '22 04:12

Gordon Wilson