Note that there are some explanatory texts on larger screens.

plurals
  1. POundefined method `items' for nil:NilClass in @cart.items.each
    primarykey
    data
    text
    <p>These are my <strong>pruduct.rb, cart.eb and item.rb</strong> </p> <pre><code>class Product &lt; ActiveRecord::Base attr_accessible :category_id, :color, :description, :price, :size, :title, :product_type, :sub_category_id, :image_url belongs_to :category belongs_to :sub_category has_many :items end </code></pre> <p><strong>Cart.rb</strong></p> <pre><code>class Cart &lt; ActiveRecord::Base has_many :items, dependent: :destroy end </code></pre> <p><strong>Item.rb</strong></p> <pre><code>class Item &lt; ActiveRecord::Base attr_accessible :cart_id, :product_id,:product belongs_to :cart belongs_to :product end </code></pre> <p>The <strong>ItemContoller</strong> </p> <pre><code>class ItemController &lt; ApplicationController def create @cart=current_cart product=Product.find(params[:product_id]) @item=@cart.items.build(product: product) redirect_to clothes_path flash.now[:success]="Product successfully added to Cart" end end </code></pre> <p>Now in my <strong>views</strong> when I want to show the <strong>cart contents</strong> like </p> <pre><code>&lt;%= @cart.items.each do |item| %&gt; </code></pre> <p>The <strong>current_cart</strong> method </p> <pre><code>def current_cart cart=Cart.find(session[:cart_id]) rescue ActiveRecord::RecordNotFound cart=Cart.create session[:cart_id]=cart.id cart end </code></pre> <p>it gives me this error</p> <blockquote> <p>undefined method `items' for nil:NilClass </p> </blockquote> <p>What is wrong here?<br> I'm following the example <strong>Agile web Developement with Rails</strong> book.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload