ActionView::Helpers::TagHelper
def content_tag(name, content_or_options_with_block = nil, options = nil, escape = true, &block)

Description

Creates an element with the given name.

Arguments

Changing a value updates the ERB and HTML.

Used as the element name. Accepts a symbol or a string. Unlike tag, builds void elements and SVG elements the same way as regular elements.
  • :textarea: as with tag, adds a newline before the content.
  • Void elements such as :br: an end tag is still added.
  • SVG elements such as :circle: an end tag is added even with no content.
  • :my_element: _ is not converted to -. For a name with -, pass the string "my-element".
A name containing spaces or similar characters raises ArgumentError.
= nil
Used as the content of the element, and escaped. Without a block, a hash is not treated as options; it is converted to a string and used as the content.
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.
= true
false
false turns off escaping for the content and the HTML attribute values. Only " in HTML attribute values still becomes &quot;.
&block= nil
With a block, uses the block's output as the content. In that case, a hash given as the second argument, content_or_options_with_block, is treated as options (the third argument's options are then ignored), and anything else is ignored.

ERB

Changes from the previous output: Added Removed Changed


          

HTML


          

Preview