hi all, Understanding function has product = Product.find(params[:id]) and @cart = find_cart . Correct me if I'm wrong but I think you always need a @ before a variable? product hasn't got this. Does this mean product isn't a variable? What is the difference between product and @cart. Thanks Stijjn def add_to_cart product = Product.find(params[:id]) @cart = find_cart @cart.add_product(product) end
on 2007-01-23 15:45
on 2007-01-23 16:11
Tarscher wrote: > hi all, > > Understanding function has product = Product.find(params[:id]) and > @cart = find_cart . Correct me if I'm wrong but I think you always need > a @ before a variable? product hasn't got this. Does this mean product > isn't a variable? > Putting @ in front of a variable makes it an instance variable accesible by other methods of that class (except class/singular methods). Without it, it's just local (from the point it's defined) till the end of the current scope. Your confusion probably stems from a little trick Rails incorporates.. It moves all instance variables from a view's controller to the correspoding view. So in the example above @cart will be accesible by the view when it's being rendered (as well as other methods in the controller) I recommend that you pick up a book on Ruby as it will greatly enhance your experience and appreciation of Rails.. Hope this helps.. ilan
on 2007-01-23 16:20
You don't always need a @. In fact, you don't always need punctuation of any kind in Ruby! The @ makes the variable an instance variable. They can be used to share a value between different methods in the same class, such as inside one of your controllers in rails, or between a controller and its views.
on 2007-01-23 16:23
Hi Stijjn, Tarscher wrote: > What is the difference between product and @cart. > def add_to_cart > product = Product.find(params[:id]) > @cart = find_cart > @cart.add_product(product) > end product is a local variable. Its value is only available for use within the add_to_cart method. @cart is an instance variable. It's value is available for use within all the methods in the controller and their views for the duration of the request/response cycle in which the variable was instantiated. hth, Bill
on 2007-01-23 16:26
On Jan 23, 2007, at 9:42 AM, Tarscher wrote: > Thanks > Stijjn > > def add_to_cart > product = Product.find(params[:id]) > @cart = find_cart > @cart.add_product(product) > end product is a local variable @cart is an instance variable On big difference for Rails has to do with the "magic" that injects the instance variables from a controller into the view being rendered. If this add_to_cart method is in a controller, the app/ view/<controller>/add_to_cart.rhtml will have access to @cart, but not product. -Rob Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com
on 2007-01-23 16:30
thanks all for the quick replies!
on 2007-01-23 16:35
Tarscher: "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, -Anthony