Created a numbered list using a loop loop

You would think that I will earn it in 10 seconds, but I spent half an hour and did not go anywhere ... Here's what I need:

  <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, for example:

  • Fred
  • Wilma, etc ...

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

+3
source share
4 answers

You can do it as follows:

<table> 
   <% @names.each_with_index do |name, i| %>
      <tr> 
         <td><%= i %></td> 
         <td><%= name %></td> 
      </tr> 
   <% end %>
</table>
+5
source

You should try using an ordered list instead of a table.

<ol> 
   <% @names.each do |name| %>
     <li><%= name %></li>  
   <% end %>
</ol>
+4
source

Ruby i++. i += 1.

+2
<table>
  <% i = 0 %>
  <% for name in @names %>
   <% i += 1 %>
   <tr>
     <td><%= i %></td>
     <td><%= name.first %></td>
   </tr>
 <% end %>
</table>
+1

Source: https://habr.com/ru/post/1751309/


All Articles