“product” is a local variable. It’s scope is local to the method in
which it is being used. Once the method is finished executing,
“product” ceases to exist.
@cart is an instance variable. It’s scope is the class in which it is
being used. If the add_to_cart method is defined in the controller,
then @cart will exist throughout the life of the controller. Instance
variables of controllers are accessible from the view. So, @cart
could be shown on a result page; “product” cannot.
I have two friendly suggestions born from my own experience learning
Rails. First, get a few Ruby-specific books. They will help with
these types of questions, act as references for functions, and the
more code you read from multiple sources, the better your code will
become. Second, in case add_to_cart from below is defined in your
controller, consider reading more about test driven development (TDD)
and try not to put business logic into the controller. Put business
logic in model objects and write unit tests for each model object. I
know it sounds like more work, but it will save you so much time
later on when you start modifying things.
Hope it helps,