0

I am working on a project and currently working on one of the views which is a page of different categories. Everything is rendering correctly however it's also putting the db info in the page.

Here is the code of my view

<div class="categories">
    <div class="container blurbs">
        <div class="cards row">
            <%= @categories.each do |c| %>
            <div class="card col-xs-4" %>
                <%= image_tag c.image, :class => "cat" %>
                <h4 class="title"><%= c.title %></h4>
            </div>
            <% end %>
        </div>
    </div>
</div>

Here is a link to a screenshot of rendered page

2 Answers 2

2

Yes, fix is:

<div class="categories">
    <div class="container blurbs">
        <div class="cards row">
            <% @categories.each do |c| %>
            <div class="card col-xs-4" %>
                <%= image_tag c.image, :class => "cat" %>
                <h4 class="title"><%= c.title %></h4>
            </div>
            <% end %>
        </div>
    </div>
</div>

Look I removed = from this <%=.. In the below line :

<% @categories.each do |c| %>

#each method returns the collection after it completed its iterations. And due to this <%=, the return value of each which is @categories printed back. But if you use <%.. only, all above things will happen, but it wouldn't print back the object @categories.

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

1 Comment

That did the trick. Crazy how a little thing like an equals sign can throw the whole thing off. Thanks for the help!
2

when you use the tags <%= ... %> whatever is within the tags gets displayed on the page. In your current view you have

<%= @categories.each do |c| %>
  <div class="card col-xs-4" %>
    <%= image_tag c.image, :class => "cat" %>
    <h4 class="title"><%= c.title %></h4>
  </div> 
<% end %>

Which displays the entirety of whatever the loop returns which is where you're getting the display. Change the tags to be <% @categories.each do |c| %> and you'll be good to go.

1 Comment

Gotcha. Thanks for the explanation about the use of <%= ... %>

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.