module ActionView::Helpers::RecordTagHelper

def content_tag_for(tag_name, single_or_multiple_records, prefix = nil, options = nil, &block)


  • ...

    produces:

    <%= content_tag_for(:li, @person, class: "bar") %>...

    with the default class name for your object. For example:
    additional HTML attributes. If you specify a :class value, it will be combined
    content_tag_for also accepts a hash of options, which will be converted to

    ...
    ...

    produces:

    <% end %>
    <%= person.last_name %>
    <%= person.first_name %>
    <%= content_tag_for(:tr, @people) do |person| %>

    For example (assuming @people is an array of Person objects):
    having to iterate through the object (using each) beforehand.
    and yield the current object to the supplied block, reducing the need for
    You can also pass an array of objects which this method will loop through

    ...

    produces:

    <%= content_tag_for(:tr, @person, :foo) do %> ...

    If you require the HTML id attribute to have a prefix, you can specify it:

    ....

    a Person object, with an id value of 123):
    would produce the following HTML (assuming @person is an instance of

    <% end %>
    <%= @person.last_name %>
    <%= @person.first_name %>
    <%= content_tag_for(:tr, @person) do %>

    that relate to the specified Active Record object. For example:
    content_tag_for creates an HTML element with id and class parameters
  • def content_tag_for(tag_name, single_or_multiple_records, prefix = nil, options = nil, &block)
      options, prefix = prefix, nil if prefix.is_a?(Hash)
      Array(single_or_multiple_records).map do |single_record|
        content_tag_for_single_record(tag_name, single_record, prefix, options, &block)
      end.join("\n").html_safe
    end

    def content_tag_for_single_record(tag_name, record, prefix, options, &block)

    for each record.
    Called by content_tag_for internally to render a content tag
    def content_tag_for_single_record(tag_name, record, prefix, options, &block)
      options = options ? options.dup : {}
      options[:class] = [ dom_class(record, prefix), options[:class] ].compact
      options[:id]    = dom_id(record, prefix)
      if block_given?
        content_tag(tag_name, capture(record, &block), options)
      else
        content_tag(tag_name, "", options)
      end
    end

    def div_for(record, *args, &block)


    Jane Bloggs

    Joe Bloggs


    produces:

    <% end %>
    <%= person.name %>
    <%= div_for(@people, class: "foo") do |person| %>

    For example:
    get iterated over and yield each record as an argument for the block.
    You can also pass an array of Active Record objects, which will then

    Joe Bloggs


    produces:

    <% end %>
    <%= @person.name %>
    <%= div_for(@person, class: "foo") do %>

    relate to the specified Active Record object. Usage example:
    Produces a wrapper DIV element with id and class parameters that
    def div_for(record, *args, &block)
      content_tag_for(:div, record, *args, &block)
    end