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 calling link_to

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

Calling url_foreither foo_urldirectly, 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 any way around this without fixing the rails?

+5
source share
2 answers

Instead of passing the path to the string, give it an array.

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

If you did not have a route in your routes file, the same link_to would create this instead:

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

The only place I could find this was this ticket .

+4
source

Any reason you need to generate urls with this path?

URL.

-3

All Articles