Rails Views: Bulletproofing dom_id

Posted 2006-07-02…

In my eternal quest to stave off DOM ID collision, I recently had to make a few small changes to the original dom_id script from the indomitable Jamis Buck.

Some examples:

>> person = Person.find(:first)
>> person.dom_id
=> "student_1" # Note: The first person is a Student
>> person.dom_id 'some_prefix'
=> "some_prefix_student_1"
>> Person.new.dom_id
=> "person_new"
>> Person.new.dom_id 'some_prefix'
=> "some_prefix_person_new"

Keep in mind that, since my original posting, a few people have released slightly-modified plugin versions of dom_id (most notably Geoffrey Grosenbach). They might already meet your needs.