0

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.

mrudult
  • 2,480
  • 3
  • 35
  • 54

2 Answers2

1

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

for eg: - 

you can pass cart id to it and find it in index cloth's action

redirect_to clothes_path, card_id: @cart.id

and in cloth's index action do 

@cart = Cart.find params[:cart_id]


create a mathod in application controller, and after u create a new cart, save its id in session, like

session[:cart] = @cart.id

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

and in view use current_cart method instead of @cart 
Sachin Singh
  • 7,107
  • 6
  • 40
  • 80
  • Actually I waant that @cart instance variable accross all controlles. so i made a new method in application_controller and added the last line of yours to it. But it's still not working. – mrudult Jul 30 '13 at 17:11
  • That is only working for ClothController. i want to access the @cart variable in all the views of the application – mrudult Jul 30 '13 at 17:50
  • @Mrudul_T read it carefully at the end, it says to create a method in application controller – Sachin Singh Jul 30 '13 at 17:52
  • @SachinSingh You know, you really could use capital letters, punctuation, full sentences, and proper formatting so we don't have to try to decipher your almost entirely unintelligible answers. – coreyward Jul 30 '13 at 17:56
  • @coreyward sorry about that. – Sachin Singh Jul 30 '13 at 17:58
  • I got that right.One thing more I want is after **adding a product to cart** I want to redirect to the same page from where the user added it. I tried saving the url with **request.url** and **request.original_url** but that gives **the page isn't redirecting properly error**. How to fix this? – mrudult Jul 30 '13 at 18:19
1

You're redirecting to clothes_path (redirect_to clothes_path) and it seems you have ClothController. This controller should contain index method to render index page. Assign current_cart to @cart there:

class ClothController < ApplicationController

  def index
    @cart = current_cart
  end

  ...

end

UPDATE

To make @cart available in all views of Cloth controller there is a method before_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#filters

class ClothController < ApplicationController
  before_filter :set_current_cart

  def index
  end

  ...

  private

  def set_current_cart
    @cart = current_cart
  end

end

current_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:

module CartHelper

  def current_cart
    # Your current cart method code
  end

end

And include it into required controllers. current_cart method will be available in all views of controllers which contain CartHelper.

class ClothController < ApplicationController
  include CartHelper

  ...
end
phts
  • 3,889
  • 1
  • 19
  • 31
  • Yeah it's working for the clothes controller. But how to make that @cart instance variable to available for all controllers or all the views – mrudult Jul 30 '13 at 17:38
  • by all views I mean all views of all the controllers. across all the application views – mrudult Jul 30 '13 at 17:49