1

My questions details are here

undefined method `items' for nil:NilClass in @cart.items.each

The extra things I added were a cart method in application controller and also made it helper_method to make it available in all views across the application

helper_method :cart

def cart
    @cart = Cart.find(session[:cart_id])
  end

When in my view I iterate over the @cart variable like this

    <%= cart.items.each do |item| %>
    <tr>
        <td><%= item.product.title%></td>
        <td><%= item.product.price%></td>
    </tr>
    <% end %>

It shows this

enter image description here

Why is printing that upper thing? I know that is the output of item.product.title and all such variables. How to remove that?

Community
  • 1
  • 1
mrudult
  • 2,243
  • 3
  • 28
  • 53

1 Answers1

1

Why is printing that upper thing?

Because you are telling it to. When you surround ruby code with <%= %> you are telling it to render the result of the enclosed code. Since you are putting it around the each block it is displaying the result of each, which is the collection it was called on.

To remove it, just use <% %> instead.

Dave Newton
  • 152,765
  • 23
  • 240
  • 286
Rob Wagner
  • 4,285
  • 13
  • 24