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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With