0

i'm very new to rails and html and i'm not sure why this piece of code is printing an array of data that i didn't ask for at the bottom of my web page. I need help with getting rid of them from my web page.

show.html.erb

<table>
  <tr>
    <th>Name</th>
  </tr>
  <%= @category.products.each do |product| %>
    <tr>
      <td><%= product.id %></td>
      <td><%= product.size %></td>
      <td><%= product.name %></td>
    </tr>
  <% end %>
</table>

<%= @category.products.each do |product| %>
  <h3><%= product.id %></h3>
  <h3><%= product.size %></h3>
  <h3><%= product.name %></h3>
<% end %>

screenshot of the website here, red arrows are pointing to the unwanted data

Any help would be greatly appreciated, thanks.

1

2 Answers 2

1

Change <%= @category... to <% @category. on both of your loops. By including the = erb is printing out the whole object on each loop (but you just want to read from the object)

Sign up to request clarification or add additional context in comments.

Comments

0

It's been awhile since I've written any ERB code, but from what I remember the <%= operator prints the result to the output. So you should be able to convert those <%= to <% so that ERB knows to execute the ruby code, but not include the result in the output.

<table>
  <tr>
    <th>Name</th>
  </tr>
  <% @category.products.each do |product| %>
    <tr>
      <td><%= product.id %></td>
      <td><%= product.size %></td>
      <td><%= product.name %></td>
    </tr>
  <% end %>
</table>

<% @category.products.each do |product| %>
  <h3><%= product.id %></h3>
  <h3><%= product.size %></h3>
  <h3><%= product.name %></h3>
<% end %>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.