ActionView::Helpers::UrlHelper
def link_to(name = nil, options = nil, html_options = nil, &block)

Description

Creates an <a>.

Arguments

Changing a value updates the ERB and HTML.

= nil
The content of the <a>, escaped. Accepts:
  • Omitted (nil): uses the href value. To pass only options, set this to nil.
  • A model object: with no options, uses the model's URL as the href and its to_s as the content. Following it with a hash, as in link_to(article, class: "x"), makes the hash the options, so the model's URL is not used.
The URL used as the href. It is passed to url_for, so the result depends on the value:
  • A string: used as the href as is. Route helpers such as article_path(1) return a URL string, so they are handled the same way.
  • A model object: builds the object's URL from the routes.
  • A hash: builds the URL from the routes ({ controller: "articles", action: "index" } gives /articles).
  • :back: the request's Referer if present, otherwise javascript:history.back(). On this site, requests to the rendering API have this page as their Referer, so the result is this page's URL.
html_options= {}
Sets the data-method attribute. If it is not :get, also adds rel="nofollow"; if rel is also given, nofollow is appended to it. data-method is an HTML attribute read by Rails UJS; in apps without Rails UJS (the default since Rails 7), it does nothing.
true adds data-remote="true". In apps without Rails UJS (the default since Rails 7), this HTML attribute does nothing.
Common options

Rails handles these options the same way in every helper. Options the HTML spec does not allow on the <a>, and ones this site considers rarely used, are not listed here.

Shared by all helpers

{active: true, hidden: false}
A hash gives the keys whose values are not false or nil, joined with spaces. An array gives its elements joined with spaces, after removing nil, false and empty strings (including whitespace-only strings).
{controller: "tags", tags_suggestions_value: ["ruby", "rails"]}
A hash adds data-* attributes, with _ in the keys converted to -. Array and hash values are converted to JSON strings.
{describedby: ["name_help", "name_error"], invalid: false}
Like :data, adds aria-* attributes. Array values are joined with spaces, leaving out nil and empty strings (nothing is added if none remain). For a hash value, the keys whose values are not false or nil are joined with spaces.
Any other options become HTML attributes of the <a>; those with a nil value are left out.

ERB

Changes from the previous output: Added Removed Changed


          

HTML


          

Preview