# Helper method problem

**URL:** <https://www.ruby-forum.com/t/helper-method-problem/149675>\
**Category:** Rails\
**Created:** [September 28, 2008, 1:41am UTC](https://www.ruby-forum.com/t/helper-method-problem/149675 "2008-09-28T01:41:31Z")\
**Posts on this page:** 4\
**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, 1:41am UTC](https://www.ruby-forum.com/t/helper-method-problem/149675/1 "2008-09-28T01:41:31Z")

</div>

In my main controller have:

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

since find\_cart is also used by another controller, I decided to move  
it from to the application\_helper.rb file too reduce redundancy.  
Rails is throwing an error saying that there is no method find\_cart.  
Any insight?

---

<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, 1:49am UTC](https://www.ruby-forum.com/t/helper-method-problem/149675/2 "2008-09-28T01:49:07Z")

</div>

On 2008-09-27 04:41, Jon wrote:

> Any insight?  
> You’ll want to put the shared method in app/controllers/application.rb  
> instead. application\_helper.rb is for shared code you use in views, not  
> controllers.

–  
Greg D.  
[http://destiney.com/](http://destiney.com/)

---

<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:23am UTC](https://www.ruby-forum.com/t/helper-method-problem/149675/3 "2008-09-28T05:23:04Z")

</div>

> You’ll want to put the shared method in app/controllers/application.rb

Unless find\_cart is used only in the one controller, then you just need  
to turn it into a before\_filter:

class MainController \< ApplicationController  
before\_filter :set\_cart

```
def index
end

def what_ever
end

private

def set_cart
   @cart = find_cart
end

```

end

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 28, 2008, 8:26am UTC](https://www.ruby-forum.com/t/helper-method-problem/149675/4 "2008-09-28T08:26:09Z")

</div>

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

The method is “check\_out”…i had it in my application.rb file  
originally,  
tried it in this one and still no change…

On Sat, Sep 27, 2008 at 8:23 PM, Phillip K. \<
