| 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 based on the options provided, default_url_options and the routes defined in routes.rb. The following options are supported:
Any other key (:controller, :action, etc.) given to url_for is forwarded to the Routes module.
Examples:
url_for :controller => 'tasks', :action => 'testing', :host=>'somehost.org', :port=>'8080' # => 'http://somehost.org:8080/tasks/testing' url_for :controller => 'tasks', :action => 'testing', :host=>'somehost.org', :anchor => 'ok', :only_path => true # => '/tasks/testing#ok' url_for :controller => 'tasks', :action => 'testing', :trailing_slash=>true # => 'http://somehost.org/tasks/testing/' url_for :controller => 'tasks', :action => 'testing', :host=>'somehost.org', :number => '33' # => 'http://somehost.org/tasks/testing?number=33'
# File vendor/rails/actionpack/lib/action_controller/url_rewriter.rb, line 52
52: def url_for(options)
53: options = self.class.default_url_options.merge(options)
54:
55: url = ''
56:
57: unless options.delete(:only_path)
58: url << (options.delete(:protocol) || 'http')
59: url << '://' unless url.match("://")
60:
61: raise "Missing host to link to! Please provide :host parameter or set default_url_options[:host]" unless options[:host]
62:
63: url << options.delete(:host)
64: url << ":#{options.delete(:port)}" if options.key?(:port)
65: else
66: # Delete the unused options to prevent their appearance in the query string.
67: [:protocol, :host, :port, :skip_relative_url_root].each { |k| options.delete(k) }
68: end
69: trailing_slash = options.delete(:trailing_slash) if options.key?(:trailing_slash)
70: url << ActionController::AbstractRequest.relative_url_root.to_s unless options[:skip_relative_url_root]
71: anchor = "##{CGI.escape options.delete(:anchor).to_param.to_s}" if options[:anchor]
72: generated = Routing::Routes.generate(options, {})
73: url << (trailing_slash ? generated.sub(/\?|\z/) { "/" + $& } : generated)
74: url << anchor if anchor
75:
76: url
77: end