Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails For Loop View

I have the following code:

<tbody>
   <%= Item.each do |item|=%>
   <tr>
      <th><%= item.rev =%></th>      <=========
      <th><%= item.name =%></th>
   </tr>
   <%= end =%>
</tbody>

However I am getting a syntax error on the inidcated line. There is data in the database(Test case). No idea what I am doing wrong.

like image 888
Blackninja543 Avatar asked Oct 31 '12 20:10

Blackninja543


2 Answers

The equals to signs you have are wrong. Try as below:

<tbody>
   <% Item.each do |item|%>
   <tr>
      <th><%= item.rev %></th>     
      <th><%= item.name %></th>
   </tr>
   <% end %>
</tbody>

The <%= should only be used for expressions that need to be evaluated.

To help understand embedded ruby see this link http://www.ruby-doc.org/docs/ProgrammingRuby/html/web.html

like image 63
adi-pradhan Avatar answered Dec 29 '22 01:12

adi-pradhan


The expression for erb tags is <% #code %>
now if we want to print that tag too then we apply <%= #code %>
i.e. only one '=' sign is used and that too on left side.
Also in line each iterator there nothing can be printed, hence no '=' sign in that line, similar is the case with tags containing 'end'.

Hence your code should look like

<tbody>
     <% Item.each do |item| %>
          <tr>
               <th><%= item.rev %></th>
               <th><%= item.name %></th>
          </tr>
     <% end %>
</tbody>

like image 26
Akshay Vishnoi Avatar answered Dec 29 '22 01:12

Akshay Vishnoi