frankwang0909 / Rails5-demo

learn Ruby on Rails with #Agile Web Development with Rails 5#
https://pragprog.com/titles/rails5/source_code
0 stars 0 forks source link

11.4 Hiding an Empty Cart #5

Open frankwang0909 opened 6 years ago

frankwang0909 commented 6 years ago

NoMethodError in Products#index

Showing /home/wxf/work/shopping/app/views/layouts/application.html.erb where line #20 raised:

undefined method `line_items' for nil:NilClass

Extracted source (around line #20):

      <div id="side">
        <div id="cart" 
          <% if @cart.line_items.empty? %>
            style="display: none;"
          <% end %>
        >

Rails.root: /home/wxf/work/shopping

solution

change the code like this:

     <% if @cart %>
          <div id="cart" 
            <% if @cart.line_items.empty? %>
              style="display: none;"
            <% end %>
          >
            <%= render @cart%>
          </div>
    <% end %>