controller issue

in my controller inventory_Controller I have a variable called @cart
that is set to @cart=find_cart in the beginning of each method. The
cart is displayed on the left hand sidemenu, so each page like
inventory/review, inventory/dvd inventory/checkout has to have @cart
defined.

For some reason @cart gets defined for the first two, but not the last
one(checkout).

The only difference between the way I am redirecting to the pageis
that the first two are by links:
                <%= link_to "review", {:action => 'review', :title =>
album.title, :itemType => album.itemType} %>
    <a href='/inventory/dvd' class='menuLink' >DVD</a>

The last one is by a button:

<%= button_to "Check Out!" , :action => :check_out %>

Mind you, the button is also generated within a partial, butI don't
think that would have any importance. Any Insight? Thanks

Jon wrote:

in my controller inventory_Controller I have a variable called @cart
that is set to @cart=find_cart in the beginning of each method. The
cart is displayed on the left hand sidemenu, so each page like
inventory/review, inventory/dvd inventory/checkout has to have @cart
defined.

Post your controller code, please.

Peace.

From experience I would use a before_filter to load the cart for you
controller...

Something like:

before_filter :initialize_cart

  private

  def initialize_cart
    if session[:cart_id]
      @cart = Cart.find(session[:cart_id])
    else
      @cart = Cart.create
      session[:cart_id] = @cart.id
    end
  end

If you have an action you don't want the cart you can use :except =>
"name"

Phillip , my inventory_controller.rb:

class InventoryController < ApplicationController

def index
@albums=Album.find(:all)
@cart=find_cart
end

def add_to_cart
itemType=params[:itemType]
productId=(params[:id]) #parameter passed in from “add to cart” submission, it’s either 1 or 2 in this case

           if itemType=='album'

                product_temp=Album.find(productId)
                dest='/inventory'
            end          
            if itemType=='dvd'
                product_temp=Dvd.find(productId)

                dest='/inventory/dvd'
            end   
           
           product=Product.new(itemType,product_temp.title,product_temp.price)
           @cart=find_cart
           @cart.add_product(product)    #add the album to the cart in the sessions

redirect_to dest

end

def check_out
@cart=find_cart
redirect_to ‘/inventory/checkOut’
end

def review
@cart=find_cart #for shopping car display in the sidebar

  @title=(params[:title])
  @itemType=(params[:itemType])
 
  if @itemType=='album' #must be a better way to reduce the amount of redundant code
      @album=Album.find_by_title(@title)

      @review=Review.new
      @reviews=@album.reviews #Review.find(:all, :conditions => ["album = ?", @title])
  end
  if  @itemType=='dvd'
      @dvd=Dvd.find_by_title(@title)

      @review=Review.new
      @reviews=@dvd.reviews #Review.find(:all, :conditions => ["album = ?", @title])
  end

end

def dvd
@cart=find_cart
@dvds=Dvd.find(:all)

end

end

my application.rb:

Filters added to this controller apply to all controllers in the application.

Likewise, all the methods added will be available for all controllers.

class ApplicationController < ActionController::Base

helper :all # include all helpers, all the time
# session :session_key => ‘_inventory_session_id’

See ActionController::RequestForgeryProtection for details

Uncomment the :secret if you’re not using the cookie session store

protect_from_forgery # :secret => ‘33dad663a17f28eb93b293523bcb06f2’

def find_cart
unless session[:cart] # if there’s no cart in the session
session[:cart] = Cart.new # add a new one

    end #for the unless
session[:cart] # return existing or new cart
   #session[:cart] ||=Cart.new #creates 1 cart session
   #can also be written more efficiently as above

end

def empty_cart

  session[:cart]=nil
  redirect_to '/inventory'
end

def find_cart
unless session[:cart] # if there’s no cart in the session
session[:cart] = Cart.new # add a new one

    end #for the unless
session[:cart] # return existing or new cart

end

def log_out
session[:login]=nil
redirect_to ‘/inventory’
end

end

find_cart is in application.rb and is used by many of the methods in my inventory_controller file. for some reason it doesn’t pull up any data when in the check_out method…

Heinbull,

I tried to use the before_filter in my Cart Model…mind you it’s not an actual sql table, it’s just a class I created. From looking at reference tools it looks like before_filter is usually used for actualy table models… but I tried it any way and I got the error “undefined method before_filter for Cart:Class”.

My Cart.rb file reads:
class Cart
attr_reader :items ,:total_price
before_filter :initialize_cart

def initialize
@items = []
@total_price=0
end

def add_product(product)

current_item = @items.find {|item| ((item.product.title == product.title) && (item.product.itemType==product.itemType)&&(item.product.itemType==product.itemType))}
#pass in the item object, returns the one being added item where item.album equals the 

#album is the title of the album in this case
#traverses through the items array, if item[album]=album if the objects match up then it adds the album
if current_item
  current_item.increment_quantity

  current_item.increment_price
  @total_price=@total_price+product.price
else
  @items << CartItem.new(product)
  @total_price=@total_price+product.price
end

end

private
def initialize_cart
unless session[:cart] # if there’s no cart in the session
session[:cart] = Cart.new # add a new one
end #for the unless
@cart=session[:cart]

end

end

any insight? it would be optimal if I could use the before_filter so i owuldn’t have to do find_cart in every page controller

All,

I put the before_filter in my inventory_controller and it worked…didn’t see that it doesn’t belong in the Cart.rb file. Thanks for the help!

Cheers

Jon