Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does dom_id and dom_class work in rails

Can anyone give a brief explaination on how dom_id and dom_class work in rails ? and when on what scenarios can we think of using it. I could not find anything on it , even in ApiDock.

Is it specific to rails only?

like image 291
Rahul Dess Avatar asked Jul 01 '14 20:07

Rahul Dess


1 Answers

dom_id and dom_class are helper methods you can use in your HTML to get consistent html id attributes and class attributes for objects.

http://api.rubyonrails.org/classes/ActionView/RecordIdentifier.html#method-i-dom_id

As an example, you might want to do the following in an index page:

<ul>
  <%= @accounts.each do |account| %>
    <li id="<%= dom_id(account) %>">
      <%= account.title %>
      <%= link_to "Edit", edit_account_path(account), class: dom_class(account, :edit) %>
    </li>
  <% end %>
</ul>

This will give you a list where each li tag has an id in the format of account_X where X is the ActiveRecord ID. You can use this ID then for javascript, etc to target the right elements.

In addition, the code above would give each edit link a class of edit_account if you want styling or selection of those common elements.

like image 169
chrisdurheim Avatar answered Sep 28 '22 07:09

chrisdurheim