# Controller issue

**URL:** <https://www.ruby-forum.com/t/controller-issue/149678>\
**Category:** Rails\
**Created:** [September 28, 2008, 3:17am UTC](https://www.ruby-forum.com/t/controller-issue/149678 "2008-09-28T03:17:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@jon](https://www.ruby-forum.com/u/jon)\
**Post date:** [September 28, 2008, 3:17am UTC](https://www.ruby-forum.com/t/controller-issue/149678/1 "2008-09-28T03:17:27Z")

</div>

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} %\>  
[DVD](https://www.ruby-forum.com/inventory/dvd)

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

---

<div class="post-metadata">

**Author:** ![jon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@jon](https://www.ruby-forum.com/u/jon)\
**Post date:** [September 28, 2008, 5:18am UTC](https://www.ruby-forum.com/t/controller-issue/149678/2 "2008-09-28T05:18:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@jon](https://www.ruby-forum.com/u/jon)\
**Post date:** [September 29, 2008, 12:00am UTC](https://www.ruby-forum.com/t/controller-issue/149678/3 "2008-09-29T00:00:15Z")

</div>

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”

---

<div class="post-metadata">

**Author:** ![jon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@jon](https://www.ruby-forum.com/u/jon)\
**Post date:** [September 29, 2008, 1:04am UTC](https://www.ruby-forum.com/t/controller-issue/149678/4 "2008-09-29T01:04:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@jon](https://www.ruby-forum.com/u/jon)\
**Post date:** [September 29, 2008, 12:59am UTC](https://www.ruby-forum.com/t/controller-issue/149678/5 "2008-09-29T00:59:59Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![jon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3bc359/32.png) [@jon](https://www.ruby-forum.com/u/jon)\
**Post date:** [September 29, 2008, 1:06am UTC](https://www.ruby-forum.com/t/controller-issue/149678/6 "2008-09-29T01:06:25Z")

</div>

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
