4
    -@things.each do |thing|
      %div.thing
        = thing

I've seen a few different techniques. But with the above HAML template, I want each thing to have a unique id from 1 to n+1 things. What's the best way to dynamically increment the id for all individual things?

Ex: For n things, I'd like the HTML to look like this.

<div class='thing' id='1'>
<div class='thing' id='2'>
...
<div class='thing' id=n+1>
BoltClock
  • 665,005
  • 155
  • 1,345
  • 1,328
Keith Johnson
  • 730
  • 2
  • 9
  • 19

2 Answers2

1
-@things.each_with_index do |thing,index|
  %div.thing{:id => "thing#{index}"}
    = thing
tihom
  • 7,733
  • 1
  • 21
  • 28
1

Something like dom_id might help you in generating unique IDs for HTML elements. It will also fix the problem of starting with a number.

sevenseacat
  • 24,021
  • 5
  • 64
  • 85