tag(name, options = nil, open = false, escape = true) public

Returns an empty HTML tag of type name which by default is XHTML compliant. Set open to true to create an open tag compatible with HTML 4.0 and below. Add HTML attributes by passing an attributes hash to options. Set escape to false to disable attribute value escaping.


You can use symbols or strings for the attribute names.

Use true with boolean attributes that can render with no value, like disabled and readonly.

HTML5 data-* attributes can be set with a single data key pointing to a hash of sub-attributes.

To play nicely with JavaScript conventions sub-attributes are dasherized. For example, a key user_id would render as data-user-id and thus accessed as dataset.userId.

Values are encoded to JSON, with the exception of strings, symbols and BigDecimals. This may come in handy when using jQuery’s HTML5-aware .data() from 1.4.3.


# => <br />

tag("br", nil, true)
# => <br>

tag("input", type: 'text', disabled: true)
# => <input type="text" disabled="disabled" />

tag("input", type: 'text', class: ["strong", "highlight"])
# => <input class="strong highlight" type="text" />

tag("img", src: "open & shut.png")
# => <img src="open & shut.png" />

tag("img", {src: "open & shut.png"}, false, false)
# => <img src="open & shut.png" />

tag("div", data: {name: 'Stephen', city_state: %w(Chicago IL)})
# => <div data-name="Stephen" data-city-state="["Chicago","IL"]" />
Show source
Register or log in to add new notes.
April 19, 2012 - (v3.1.0 - v3.2.1)
0 thanks

Missed close tag

At the page http://apidock.com/rails/ActionView/Helpers/TagHelper/tag

<tt>.data()</tt> should be instead of <tt>.data()<tt>