I am also new and was getting a little lost (although I've learned a
tremendous amount) with local variables and things that I percieved as
So when does product refer to my product model? And what exactly is
the difference between the local variable w/ @ and without. I thought
there weren't a whole lot of variables without the @.
If I remember correctly, the convention is that the class name starts with a capital letter. So, Product is a class and product is the object. If you find that confusing, you could do things as you might in other languages:
my_product = Product.find(product_id)
but this looks ugly once you get used to the other notation
A local variable without the @ is simply that - a variable local to the specific method in your controller. With the '@', it is an instance variable. Without going into the meanings of instance variables, the "Rails" meaning (not the Ruby one) for the variable is that it can be seen by code in both the controller and the view.
So, a temporary variable in a method in your controller would be without a '@' - any variable which you would like to display in the view must be an instance variable.
If we look at the code in the original post,
product = Product.find(params[:id])
@cart = find_cart
redirect_to(:action => 'display_cart')
we can see that the details of the product that you find do not need to be displayed in the view. But, the cart details must be shown in that view. That's why product (without @) and @cart (with @).
Lastly, params (though you didn't ask) is a hash that communicates parameters from to an action. Think of it as input parameters to the controller's method.
In summary, the controller method picks up the inputs from the params hash, processes it in local variables (without @) and writes its output to instance variables (with @). The view that is rendered as a result of the action reads values from the instance variables of the controller method and uses those values for rendering the output. When the user interacts with the view and clicks on something, it will send a request to a controller armed with relevant parameters (like the values of fields entered in a form) stored in the params hash - Full Circle!
Hope this helps.