ActionView::Helpers::TagHelper
def <element name>(content = nil, escape: true, **options, &block)

Description

Creates an element by calling a method named after it (such as tag.div).

Arguments

Changing a value updates the ERB and HTML.

The method name, used as the element name. Depending on the name:
  • Regular elements such as div: uses the content argument as the content and adds an end tag.
  • textarea: adds a newline before the content. Browsers skip a newline right after the start tag, so this keeps a leading newline in the content from being lost.
  • Void elements such as br: closes with > and adds no end tag. They do not take the content argument, so passing it raises ArgumentError. A block is ignored.
  • SVG elements such as circle: closes with /> if neither content nor a block is given.
  • A name not in Rails' list of HTML elements: creates an element with _ in the name converted to -.
= nil
Used as the content of the element, and escaped.
= true
false
false turns off escaping for the content argument and the HTML attribute values. Only " in HTML attribute values still becomes &quot;.
options= {}
Common options

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

Shared by all helpers

true adds disabled="disabled"; false omits it. The same goes for every HTML attribute in Rails' list (BOOLEAN_ATTRIBUTES), such as readonly, required and autofocus: any value other than false or nil adds it as attr="attr".
{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.
["section-user", "name"]
An array sets autocomplete to its elements joined with spaces.
/[a-z]+/i
A regular expression sets pattern to the part between / and /. Flags such as i are not included.
Any other options become HTML attributes of the <div>; those with a nil value are left out.
&block= nil
With a block, uses the block's output as the content. If the content argument is also given, content is ignored.

ERB

Changes from the previous output: Added Removed Changed


          

HTML


          

Preview