In Rails, beware that url_for has slightly different default behavior depending on whether you call it from a template or a controller. When url_for is called in a template (ActionView or Helper):
When url_for is called in a controller (ActionController):
There are times in a view when you want the unescaped version (such as putting into a Javascript variable, or generating a plain text file). Simply pass escape => false:
- urls are escaped by default (ampersands become &)
- returns relative path by default, unless a host is specified
> url_for( { :controller => "test",
:action => "me",
:foo => 1,
:bar => 2 } )
/test/me?foo=1&bar=2
When url_for is called in a controller (ActionController):
- urls are not escaped by default
- full path is returned unless only_path is specified
> url_for( { :controller => "test",
:action => "me",
:foo => 1,
:bar => 2 } )
http://example.com/test/me?foo=1&bar=2
There are times in a view when you want the unescaped version (such as putting into a Javascript variable, or generating a plain text file). Simply pass escape => false:
> url_for( { :controller => "test",
:action => "me",
:foo => 1,
:bar => 2,
:escape => false } )
http://example.com/test/me?foo=1&bar=2