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
          @[EMAIL PROTECTED] #Review.find(:all, :conditions => ["album
= ?", @title])
      end
      if  @itemType=='dvd'
          @dvd=Dvd.find_by_title(@title)
          @review=Review.new
          @[EMAIL PROTECTED] #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....

On Sun, Sep 28, 2008 at 2:14 PM, heimdull <[EMAIL PROTECTED]> wrote:

>
> 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"
>
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to