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
@[EMAIL PROTECTED]
else
@items << CartItem.new(product)
@[EMAIL PROTECTED]
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
On Sun, Sep 28, 2008 at 3:59 PM, Jon Liu <[EMAIL PROTECTED]> wrote:
> 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
-~----------~----~----~----~------~----~------~--~---