views:

75

answers:

1

I'm wondering what is the difference between these two methods: ActionView::Helpers::UrlHelper.url_for and ActionController::UrlWriter.url_for?

+1  A: 

The version in UrlHelper is the one that is available inside your views. Here is its source:

def url_for(options = {})
  options ||= {}
   url = case options
   when String
     escape = true
     options
   when Hash
     options = { :only_path => options[:host].nil? }.update(options.symbolize_keys)
     escape  = options.key?(:escape) ? options.delete(:escape) : true
     @controller.send(:url_for, options)
   when :back
     escape = false
     @controller.request.env["HTTP_REFERER"] || 'javascript:history.back()'
   else
     escape = false
     polymorphic_path(options)
   end

   escape ? escape_once(url) : url
end

In the case when the parameter passed to this url_for is a Hash it delegates to controller.url_for which in turn delegates to ActionController::UrlWriter.url_for.

The version in ActionView::Helpers::UrlHelper.url_for then adds some extra behaviour for when it is called with other types of arguments. In the case when a String is passed it is escaped for you. When called with the symbol :back which either links to the referer if available or falls back to a JavaScript history.back() call.

mikej
So url_for available in views is just a wrapper for ActionController::UrlWriter.url_for?
Eimantas
In the case when you call it with a Hash, yes. It does provide a few bits of extra behaviour as covered in the answer.
mikej