Module | ActionController::UrlWriter |
In: |
vendor/rails/actionpack/lib/action_controller/url_rewriter.rb
|
Write URLs from arbitrary places in your codebase, such as your mailers.
Example:
class MyMailer include ActionController::UrlWriter default_url_options[:host] = 'www.basecamphq.com' def signup_url(token) url_for(:controller => 'signup', action => 'index', :token => token) end end
In addition to providing url_for, named routes are also accessible after including UrlWriter.
Generate a url with the provided options. The following special options may effect the constructed url:
* :host Specifies the host the link should be targetted at. This option must be provided either explicitly, or via default_url_options. * :protocol The protocol to connect to. Defaults to 'http' * :port Optionally specify the port to connect to.
# File vendor/rails/actionpack/lib/action_controller/url_rewriter.rb, line 40 40: def url_for(options) 41: options = self.class.default_url_options.merge(options) 42: 43: url = '' 44: unless options.delete :only_path 45: url << (options.delete(:protocol) || 'http') 46: url << '://' 47: 48: raise "Missing host to link to! Please provide :host parameter or set default_url_options[:host]" unless options[:host] 49: url << options.delete(:host) 50: url << ":#{options.delete(:port)}" if options.key?(:port) 51: else 52: # Delete the unused options to prevent their appearance in the query string 53: [:protocol, :host, :port].each { |k| options.delete k } 54: end 55: anchor = "##{options.delete(:anchor)}" if options.key?(:anchor) 56: url << Routing::Routes.generate(options, {}) 57: return "#{url}#{anchor}" 58: end