Experiences on the Front Lines of User Interfaces and Web Development

Rails url_for differences between controller and view/helper versions

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):
  • urls are escaped by default (ampersands become &)
  • returns relative path by default, unless a host is specified
When url_for is called in a controller (ActionController):
  • urls are not escaped by default
  • full path is returned unless only_path is specified
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:
comments powered by Disqus