These are my pruduct.rb, cart.eb and item.rb
class Product < 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
Cart.rb
class Cart < ActiveRecord::Base
has_many :items, dependent: :destroy
end
Item.rb
class Item < ActiveRecord::Base
attr_accessible :cart_id, :product_id,:product
belongs_to :cart
belongs_to :product
end
The ItemContoller
class ItemController < 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
Now in my views when I want to show the cart contents like
<%= @cart.items.each do |item| %>
The current_cart method
def current_cart
cart=Cart.find(session[:cart_id])
rescue ActiveRecord::RecordNotFound
cart=Cart.create
session[:cart_id]=cart.id
cart
end
it gives me this error
undefined method `items' for nil:NilClass
What is wrong here?
I'm following the example Agile web Developement with Rails book.
after you redirect to cloths_path from ItemController's create action @cart instance variable will not be available to cloth's controller index action. to need to reset it some how in index action
You're redirecting to
clothes_path
(redirect_to clothes_path
) and it seems you haveClothController
. This controller should containindex
method to render index page. Assigncurrent_cart
to@cart
there:UPDATE
To make
@cart
available in all views ofCloth
controller there is a methodbefore_filter
where you can set@cart
variable. You can add this definitions in your required controllers. For more details - http://guides.rubyonrails.org/action_controller_overview.html#filterscurrent_cart
method should be implemented as helper to be available in all controllers. (http://guides.rubyonrails.org/form_helpers.html)UPDATE 2
Implement a helper in
/app/helpers/cart_helper.rb
:And include it into required controllers.
current_cart
method will be available in all views of controllers which containCartHelper
.