0

I am trying to display the columns of a model as part of a table that shows the results of a query. I want to do this dynamically with an each loop in Ruby.

<table>
    <tr>
        <%= Terminology.column_names.each do |c| %>
        <th> <% c.humanize %> </th> 
        <% end %>
    </tr>
</table>

When I display this, it shows 30 or so empty header tags, then the array of column names is put between the last header and the end of the row. What is going on here?

jgolfman
  • 43
  • 7

3 Answers3

4

I believe your missing the = sign in <% c.humanize %>.

Should be <%= c.humanize %>. The = will display the value.

Also, you need to remove the = in <%= Terminology.column_names.each do |c| %>.

That should be <% Terminology.column_names.each do |c| %>.

Graham S.
  • 1,480
  • 1
  • 20
  • 28
  • This works, but now the array is still visible above the table, and in the HTML, it is in the same position as before. How do I get rid of this? – jgolfman Jun 13 '17 at 21:53
  • I edited my post probably before you tried again. Did you remove the `=` in `<%= Terminology.column_names.each do |c| %>`? – Graham S. Jun 13 '17 at 21:54
  • Thanks, I didn't realize there was a difference between the different tags. I thought the <%= was just to open a script like in PHP. – jgolfman Jun 13 '17 at 21:57
  • Glad I could help. And yes, in Rails, `<%= some_var %>` actually prints the value to the screen, whereas `<% some_var %>` will not. And actually, the new PHP syntax now optionally follows this pattern I believe with the same consequences `= $some_var; ?>` prints and ` $some_var; ?>` does not. – Graham S. Jun 13 '17 at 22:01
2

Try this:

<% Terminology.column_names.each do |c| %>
<th> <%= c.humanize %> </th> 
<% end %>

Check this related post

Alejandro Montilla
  • 2,626
  • 3
  • 31
  • 35
2

Try This

<table>
    <tr>
        <%@variable_name.column_names.each do |c| %>
        <th> <%= c.humanize %> </th> 
        <% end %>
    </tr>
</table>