---
title: "Unique ID for DOM in Rails"
description: "Rails has many great view helpers, one of which is dom\\id:*"
canonical: https://oozou.com/til/unique-id-for-dom-in-rails-75
locale: en
date: 2020-10-14
topic: ruby-on-rails
---

# Unique ID for DOM in Rails

> Rails has many great view helpers, one of which is dom\id:*

Published: 2020-10-14 · Topic: [ruby on rails](https://oozou.com/til-topics/ruby-on-rails) ([markdown](https://oozou.com/til-topics/ruby-on-rails.md))

Rails has many great view helpers, one of which is *dom\_id:*

```
dom_id(Post.find(12))       <em># => "post_23"</em>
dom_id(Post.new)            <em># => "new_post"
language-rails</em>
```

You can also provide a prefix:

```
dom_id(Post.find(23), :edit) <em># => "edit_post_23"</em>
dom_id(Post.new, :custom)    <em># => "custom_post"
language-rails</em>
```

[Official Rails doc](https://edgeapi.rubyonrails.org/classes/ActionView/RecordIdentifier.html#method-i-dom_id)

---

Contact OOZOU: hello@oozou.com · +66 2544 0000 · https://oozou.com/contact

More for AI agents: https://oozou.com/llms.txt · every page has a markdown twin at its URL plus ".md"
