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
  • 700,868
  • 160
  • 1,392
  • 1,356
Keith Johnson
  • 730
  • 2
  • 9
  • 19

2 Answers2

1
-@things.each_with_index do |thing,index|
  %div.thing{:id => "thing#{index}"}
    = thing
tihom
  • 7,923
  • 1
  • 25
  • 29
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,699
  • 6
  • 63
  • 88