Module: ActionController::UrlWriter
- Defined in:
- lib/action_controller/url_rewriter.rb
Overview
Write URLs from arbitrary places in your codebase, such as your mailers.
Example:
class MyMailer
include ActionController::UrlWriter
[: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.
Class Method Summary collapse
-
.included(base) ⇒ Object
:nodoc:.
Instance Method Summary collapse
-
#url_for(options) ⇒ Object
Generate a url with the provided options.
Class Method Details
.included(base) ⇒ Object
:nodoc:
26 27 28 29 30 |
# File 'lib/action_controller/url_rewriter.rb', line 26 def self.included(base) #:nodoc: ActionController::Routing::Routes.named_routes.install base base.mattr_accessor :default_url_options base. ||= end |
Instance Method Details
#url_for(options) ⇒ Object
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.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/action_controller/url_rewriter.rb', line 40 def url_for() = self.class..merge() url = '' unless .delete :only_path url << (.delete(:protocol) || 'http') url << '://' raise "Missing host to link to! Please provide :host parameter or set default_url_options[:host]" unless [:host] url << .delete(:host) url << ":#{.delete(:port)}" if .key?(:port) else # Delete the unused options to prevent their appearance in the query string [:protocol, :host, :port].each { |k| .delete k } end url << Routing::Routes.generate(, {}) return url end |