url_for
- 1.0.0
- 1.1.6
- 1.2.6
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.8
- 3.0.0 (0)
- 3.0.9 (-2)
- 3.1.0 (26)
- 3.2.1 (4)
- 3.2.8 (0)
- 3.2.13 (0)
- 4.0.2 (19)
- 4.1.8 (0)
- 4.2.1 (0)
- 4.2.7 (0)
- 4.2.9 (0)
- 5.0.0.1 (38)
- 5.1.7 (0)
- 5.2.3 (0)
- 6.0.0 (4)
- 6.1.3.1 (0)
- 6.1.7.7 (0)
- 7.0.0 (0)
- 7.1.3.2 (10)
- 7.1.3.4 (0)
- What's this?
url_for(options = nil)
public
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 to false.
-
:protocol - The protocol to connect to. Defaults to ‘http’.
-
:host - Specifies the host the link should be targeted at. If :only_path is false, this option must be provided either explicitly, or via default_url_options.
-
:subdomain - Specifies the subdomain of the link, using the tld_length to 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 the tld_length to split the domain from the host.
-
:tld_length - Number of labels the TLD id composed of, only used if :subdomain or :domain are supplied. Defaults to ActionDispatch::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.
-
:params - The query parameters 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'
Missing routes keys may be filled in from the current request’s parameters (e.g. :controller, :action, :id and any other parameters that are placed in the path). Given that the current action has been reached through GET /users/1:
url_for(only_path: true) # => '/users/1' url_for(only_path: true, action: 'edit') # => '/users/1/edit' url_for(only_path: true, action: 'edit', id: 2) # => '/users/2/edit'
Notice that no :id parameter was provided to the first url_for call and the helper used the one from the route’s path. Any path parameter implicitly used by url_for can always be overwritten like shown on the last url_for calls.
How to specify :only_path when non-hash options
When passing in an object, as opposed to a hash, you can’t do this because url_for accepts one argument:
url_for(post, :only_path => true)
Instead, do this:
polymorphic_url(object, :routing_type => :path)
Model objects routed with :as
When providing a model object, url_for will not work if the model’s routes are named using the :as option. You can instead use the named helper methods (posts_path, post_path(:id), etc.).
Passing an object as argument
It is possible to pass an instance of a record to the method. See the documentation of polymorphic_url (http://apidock.com/rails/ActionDispatch/Routing/PolymorphicRoutes).