# Whats the difference? Function order

**URL:** <https://www.ruby-forum.com/t/whats-the-difference-function-order/111249>\
**Category:** Rails\
**Created:** [August 4, 2007, 10:36am UTC](https://www.ruby-forum.com/t/whats-the-difference-function-order/111249 "2007-08-04T10:36:58Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 4, 2007, 10:36am UTC](https://www.ruby-forum.com/t/whats-the-difference-function-order/111249/1 "2007-08-04T10:36:58Z")

</div>

I have been baffled by this for a while?  
I have been going through some tutorials and got stuck on this.  
The following gives me an error in the view “You have a nil object  
when you didn’t expect it” for “display\_cart”. It’s saying the  
variable “@items” is nil in the view.  
But if i reorder the functions and put “display\_cart” before  
“find\_cart”, it works.  
Does the order of the functions matter?

class StoreController \< ApplicationController

def index  
@products = Product.salable\_items  
end

def add\_to\_cart  
product = Product.find(params[:id])  
@cart = find\_cart  
@cart.add\_product(product)  
redirect\_to(:action =\> ‘display\_cart’)  
end

private  
def find\_cart  
session[:cart] ||= Cart.new  
end

def display\_cart  
@cart = find\_cart  
@items = @cart.items  
end

end

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [August 4, 2007, 2:02pm UTC](https://www.ruby-forum.com/t/whats-the-difference-function-order/111249/2 "2007-08-04T14:02:20Z")

</div>

Hi –

On Sat, 4 Aug 2007, … wrote:

> I have been baffled by this for a while?  
> I have been going through some tutorials and got stuck on this.  
> The following gives me an error in the view “You have a nil object  
> when you didn’t expect it” for “display\_cart”. It’s saying the  
> variable “@items” is nil in the view.  
> But if i reorder the functions and put “display\_cart” before  
> “find\_cart”, it works.  
> Does the order of the functions matter?

No, but their relation to the “private” directive does. If  
display\_cart is private, then the action won’t be executed; instead,  
the template will be rendered directly, and since the action was  
skipped, @items won’t be set.

So you’ll need to move display\_cart above “private”, or declare it  
public. (I’d move it; it gets messy if you start declaring access for  
methods individually.)

David

–

- Books:  
RAILS ROUTING (new! [http://www.awprofessional.com/title/0321509242](http://www.awprofessional.com/title/0321509242))  
RUBY FOR RAILS ([Ruby for Rails](http://www.manning.com/black))
- Ruby/Rails training  
& consulting: Ruby Power and Light, LLC ([http://www.rubypal.com](http://www.rubypal.com))
