views:

43

answers:

4

You would think I would get this done in 10 seconds, but I've spent 1/2 hour and am getting nowhere..Here is what I have/want:

  <table>
     <% i=0 %>
     <% for name in @names%>
       <% i++ %>
       <tr>
  <td><%= "#{i}" %></td>
  <td><%= name.first %>"></td>
       </tr>
  </table>

Yes, all I want is a numbered list of names, like:

  1. fred
  2. wilma etc...

The error I get is: compile error /blah/_names.html.erb:13: syntax error, unexpected ';' ; i++ ; @output_buffer.concat "\n\t\t <td>"

+2  A: 

You can do like this

<table> 
   <% @names.each_with_index do |name, i| %>
      <tr> 
         <td><%= i %></td> 
         <td><%= name %></td> 
      </tr> 
   <% end %>
</table>
j.
There is an extra `>` before the second `</td>`.
kiamlaluno
Missed that. Thanks! :]
j.
Thanks, i knew there was more then one way of doing this.
rtfminc
You're welcome...
j.
Doesn't add the dots after the numbers (An ordered list will do this for you)
Atømix
+1  A: 

There's no i++ in Ruby. Try i += 1 instead.

Chris Bunch
Aaaaaaaaaaaaaarrrrrrrrggggggghhhhh! Someone shoot me, I could not figure out what was wrong! Doh! That got it working, thanks!
rtfminc
A: 
<table>
  <% i = 0 %>
  <% for name in @names %>
   <% i += 1 %>
   <tr>
     <td><%= i %></td>
     <td><%= name.first %></td>
   </tr>
 <% end %>
</table>
kiamlaluno
This is what I was trying to do. thanks.
rtfminc
+2  A: 

You should try using an Ordered List instead of a Table

<ol> 
   <% @names.each do |name| %>
     <li><%= name %></li>  
   <% end %>
</ol>
Agree! Unless you need the number to be read or something, an ordered list is the semantic and best way to do a numbered list in html. This way, if you use javascript to remove items from the list, the list should still be ordered.
Atømix