instance method
url_for
Ruby on Rails 4.2.11.3
Since v3.0.20Signature
url_for(options = nil)
Generate a url based on the options provided, default_url_options and the routes defined in routes.rb. The following options are supported:
-
:only_path- If true, the relative url is returned. Defaults tofalse. -
:protocol- The protocol to connect to. Defaults to ‘http’. -
:host- Specifies the host the link should be targeted at. If:only_pathis false, this option must be provided either explicitly, or viadefault_url_options. -
:subdomain- Specifies the subdomain of the link, using thetld_lengthto split the subdomain from the host. If false, removes all subdomains from the host part of the link. -
:domain- Specifies the domain of the link, using thetld_lengthto split the domain from the host. -
:tld_length- Number of labels the TLD id composed of, only used if:subdomainor:domainare supplied. Defaults toActionDispatch::Http::URL.tld_length, which in turn defaults to 1. -
:port- Optionally specify the port to connect to. -
:anchor- An anchor name to be appended to the path. -
:trailing_slash- If true, adds a trailing slash, as in “/archive/2009/” -
:script_name- Specifies application path relative to domain root. If provided, prepends application path.
Any other key (:controller, :action, etc.) given to url_for is forwarded to the Routes module.
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'
url_for controller: 'tasks', action: 'testing', host: 'somehost.org', script_name: "/myapp"
# => 'http://somehost.org/myapp/tasks/testing'
url_for controller: 'tasks', action: 'testing', host: 'somehost.org', script_name: "/myapp", only_path: true
# => '/myapp/tasks/testing'
Parameters
-
optionsopt = nil
Source
# File actionpack/lib/action_dispatch/routing/url_for.rb, line 150
def url_for(options = nil)
case options
when nil
_routes.url_for(url_options.symbolize_keys)
when Hash
route_name = options.delete :use_route
_routes.url_for(options.symbolize_keys.reverse_merge!(url_options),
route_name)
when String
options
when Symbol
HelperMethodBuilder.url.handle_string_call self, options
when Array
components = options.dup
polymorphic_url(components, components.extract_options!)
when Class
HelperMethodBuilder.url.handle_class_call self, options
else
HelperMethodBuilder.url.handle_model_call self, options
end
end
Defined in actionpack/lib/action_dispatch/routing/url_for.rb line 150
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActionDispatch::Routing::UrlFor