Trying to understand something about instance variables.

An instance variable will be available to embedded ruby calls in your rhtml templates; a local variable will only be available within the method it’s defined in.

In the example from AWDWR you quoted:

`> def add_to_cart

@cart = find_cart

product = Product.find(params[:id])

@cart.add_product(product)

end

`

@cart is an instance variable (denoted with an @ symbol), because it’s an instance of the Cart object. Product is a local variable, its scope is local to the action (method) add_to_cart. @cart can be exposed in web pages, but product cannot.

Further down AWDWR shows the instance variable @cart exposed in the add_to_cart.rhtml template:

Your Pragmatic Cart

    <% for item in @cart.items %>

  • <%= item.title %>
  • <% end %>

The rule of thumb is: to make data visible to your user, use an instance variable. The AWDWR book does a much better job of illustrating these details.

Hope this helps!

Regards,

Dave