Repeated, but unformatted, table data

Hello,      When printing the data from a database table to an html table in index.html.erb, there is a strange output in which the entire table is printed sequentially and unformatted, followed by the actual formatted data. The formatted data is correct, along with its headers and some links.      After the html headers are printed in a table row:

<table>   <tr>     <th>First name</th> ... then the table data is printed:

<%= @people.each do |person| %>   <tr>     <td><%= person.first_name %></td>     <td><%= person.middle_name %></td> ... and by the process of elimination, the offending line is <%= @people.each do |person| %> as the table data dump occurs even when it is alone (except for the following <% end %>). But, of course, I can't do without it or I wouldn't get all of the table data.      How do I fix that line, or what is the replacement which still iterates through the table?      Thanks,           Barney

Hello,     When printing the data from a database table to an html table in index.html.erb, there is a strange output in which the entire table is printed sequentially and unformatted, followed by the actual formatted data. The formatted data is correct, along with its headers and some links.     After the html headers are printed in a table row:

<table> <tr>    <th>First name</th> ... then the table data is printed:

<%= @people.each do |person| %> <tr>    <td><%= person.first_name %></td>    <td><%= person.middle_name %></td> ... and by the process of elimination, the offending line is <%= @people.each do |person| %> as the table data dump occurs even when it

That should be <% @people.each do |person| %>

Or even better, <%- @people.each do |person| -%> which will not add an empty line to your output HTML.

Walter

Thanks Walter! That did the trick! Do you have a link where I can read about the differences between < %= ... , <%-... and <% ? I haven't spotted that in the "Agile Web Development with Rails" book I'm reading.       Barney

Look in the index for "ERb" -- my old second edition covers this on page 40-43.

HTH,

It's pretty basic:

<%= foo %> means print whatever is inside to the output. In Rails 2.3, it means literally print it whatever it is. In Rails 3 and up, it means html-escape it and print it.

<% foo %> means this is an instruction line, a bit of Ruby mixed in among the HTML. You use this to start and end your loops over a collection, or put an island of logic or data in the view. If you put a puts command in there, then it's functionally the same thing as the first one, but if you don't explicitly print anything, it's just a program step.

<%- foo -%> means this is an instruction line, but in addition, don't leave an extra line of whitespace where it was, just do the Ruby instruction and don't add any whitespace.

Walter

Ah, yes, on p. 21 it says that <%= will execute the Ruby, HTML-escape it, convert it to a string and print it to the screen. Unfortunately, just because I read something doesn't mean I remember it! Thanks to Walter and Hassan for reminding me!           Barney