module ActionView::RecordIdentifier

def dom_class(record_or_class, prefix = nil)

dom_class(Person, :edit) # => "edit_person"
dom_class(post, :edit) # => "edit_post"

If you need to address multiple instances of the same class in the same view, you can prefix the dom_class:

dom_class(Person) # => "person"
dom_class(post) # => "post"

The DOM class convention is to use the singular form of an object or class.
def dom_class(record_or_class, prefix = nil)
  singular = model_name_from_record_or_class(record_or_class).param_key
  prefix ? "#{prefix}#{JOIN}#{singular}" : singular
end

def dom_id(record_or_class, prefix = nil)

dom_id(Post, :custom) # => "custom_post"
dom_id(Post.find(45), :edit) # => "edit_post_45"

If you need to address multiple instances of the same class in the same view, you can prefix the dom_id:

dom_id(Post) # => "new_post"
dom_id(Post.find(45)) # => "post_45"

If no id is found, prefix with "new_" instead.
The DOM id convention is to use the singular form of an object or class with the id following an underscore.
def dom_id(record_or_class, prefix = nil)
  raise ArgumentError, "dom_id must be passed a record_or_class as the first argument, you passed #{record_or_class.inspect}" unless record_or_class
  record_id = record_key_for_dom_id(record_or_class) unless record_or_class.is_a?(Class)
  if record_id
    "#{dom_class(record_or_class, prefix)}#{JOIN}#{record_id}"
  else
    dom_class(record_or_class, prefix || NEW)
  end
end

def record_key_for_dom_id(record) # :doc:

:doc:
make sure yourself that your dom ids are valid, in case you override this method.
method that replaces all characters that are invalid inside DOM ids, with valid ones. You need to
overwritten version of the method. By default, this implementation passes the key string through a
on the default implementation (which just joins all key attributes with '_') or on your own
you should write a helper like 'person_record_from_dom_id' that will extract the key either based
If you need to read back a key from a dom_id in order to query for the underlying database record,
This can be overwritten to customize the default generated string representation if desired.
Returns a string representation of the key attribute(s) that is suitable for use in an HTML DOM id.
def record_key_for_dom_id(record) # :doc:
  key = convert_to_model(record).to_key
  key && key.all? ? key.join(JOIN) : nil
end