ActionView::RoutingUrlFor
def url_for(options = nil)

Description

Returns a URL as a string (only the path by default). Helpers such as link_to and button_to use it to build the URLs they are given.

This site renders with GET /articles?page=2, shown above the ERB, as the current request. Besides a hash, it accepts:
  • A string: returned as is.
  • nil: the current request's path (/articles), without the query string.
  • A model object (Article.find(1)), an array ([Article.find(1), comment]), a symbol (:articles) or a class (Article): finds the named route helper (such as article_path) from the routes and calls it.
  • :back: the request's Referer if present, otherwise javascript:history.back().

Arguments

Changing a value updates the ERB and HTML.

options= {}
"articles"
The controller to build the URL for. Defaults to the current request's controller (articles).
"show"
The action to build the URL for. Defaults to the current request's action (index), or to index when controller differs from the current request's.
1
Fills the route's :id segment (/articles/1). Leaving it out while controller and action point to articles#show raises ActionController::UrlGenerationError, as no route matches without :id. For a route without :id (articles#index), it goes into the query string (/articles?id=1).
Fills the route's (.:format) segment (/articles/1.json).
"comments"
Appended after #.
{page: "3"}
Each key and value in the hash is added to the query string (?page=3). Unlike other options, a key named after a route segment (such as :id) also goes into the query string instead of the segment. If another option has the same key, this value wins.
true appends / to the path (/articles/1/). Ignored when format is also given.
"/blog"
Prepended to the path (/blog/articles/1). Used when the app is mounted under a path such as /blog. Defaults to the current request's (the path the app is mounted under; empty on this site), and a given value replaces it.
false returns a URL with the host (http://…/articles/1). Without host, the host is the current request's (on this site, this page's host). Defaults to false when host is given, otherwise true.
"example.com"
The host of the URL. If only_path is omitted, giving this returns a URL with the host. Even if written as example.com:3000 or https://example.com, the port and https:// are not used (the current request's are); pass them with port and protocol.
"https"
The part before :// in a URL with a host (https://…). Writing https: or https:// gives the same result. "//" or false gives //example.com/…. Defaults to the current request's (http or https). Ignored when only the path is returned (when neither host nor only_path: false is given).
8080
The :8080 after the host in a URL with a host. Defaults to the current request's port (left out for 80 and 443). Ignored when only the path is returned.
"www"
Replaces the subdomain of the host (blog.example.com becomes www.example.com). The subdomain is everything before the last tld_length + 1 labels of the host (2 by default). Ignored when only the path is returned.
"example.org"
Replaces the part of the host after the subdomain (blog.example.com becomes blog.example.org). Ignored when only the path is returned.
"alice"
Together with password, prepends alice:secret@ to the host. On its own, it goes into the query string like any other option. Ignored, even with both, when only the path is returned.
"secret"
Together with user, prepends alice:secret@ to the host. On its own, it goes into the query string like any other option. Ignored, even with both, when only the path is returned.
Any other options become the query string (page: 2 gives ?page=2), unless the route has a segment with that name (such as :id). tld_length (how many labels at the end of the host subdomain and domain treat as the top-level domain; defaults to 1), original_script_name, path_params and use_route are used to build the URL and never become the query string.

ERB

Changes from the previous output: Added Removed Changed

Current request
GET /articles?page=2

          

HTML