Hi there, I am doing the "depot" application from Agile Web Development With Rails book - and there is a lot of copy-and-paste. And I have to say, as a beginner in both Ruby and Rails, I don''t understand quite a lot of things. Let''s talk about this thing - there is a method called "add_to_cart". I don''t understand much, what it''s doing... (it''s in class StoreController < ApplicationController) def add_to_cart product = Product.find(params[:id]) @cart = find_cart @cart.add_product(product) redirect_to(:action => ''display_cart'') end So, I have a few questions. 1) what exactly is product? Why it''s not @product? Is it global or local variable? 2) what exactly is Product? I''m not sure, but I think its the only instance of Product class, right? Then why is the name of instance and name of the class the same? 3) What exactly is this params thing? 4) what exactly is :id? I don''t understand, in which cases you use colon and in which not - why I have to use it in Product.find, but not in add_product? 5) what exactly does => mean? Why I can''t use ordinary = ? Sorry if I ask too much, I just want to understand this language and this framework. Kaja Bilek --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
running idiot wrote:> Hi there, > > I am doing the "depot" application from Agile Web Development With > Rails book - and there is a lot of copy-and-paste. And I have to say, > as a beginner in both Ruby and Rails, I don''t understand quite a lot > of things. > > Let''s talk about this thing - there is a method called "add_to_cart". > I don''t understand much, what it''s doing... (it''s in class > StoreController < ApplicationController) > > > def add_to_cart > product = Product.find(params[:id]) > @cart = find_cart > @cart.add_product(product) > redirect_to(:action => ''display_cart'') > end > > > So, I have a few questions. > > 1) what exactly is product? Why it''s not @product? Is it global or > local variable?product is a local variable used to store the result of Product.find(...)> 2) what exactly is Product? I''m not sure, but I think its the only > instance of Product class, right? Then why is the name of instance and > name of the class the same? >Product.find(...) is calling the ''Product'' _class_ method ''find'' - it''s not an instance> 3) What exactly is this params thing? >params is the parameter hash passed when the request is made, e.g. when you submit a form your data comes in through params - ditto when you make a GET request via an URL (/user/23 for example)> 4) what exactly is :id? I don''t understand, in which cases you use > colon and in which not - why I have to use it in Product.find, but not > in add_product? >the preceding colon signifies a symbol - there''s some good info on ruby symbols here => http://glu.ttono.us/articles/2005/08/19/understanding-ruby-symbols && here => http://www.troubleshooters.com/codecorn/ruby/symbols.htm> 5) what exactly does => mean? Why I can''t use ordinary = ? >key=>value, pretty common across different languages> Sorry if I ask too much, I just want to understand this language and > this framework. > >perhaps have a look @ http://pine.fm/LearnToProgram/ - a tasty little morsel that should help - not the most comprehensive answer but it''s 2am and i''ve been dipping the whiskey :P> Kaja Bilek > > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
jamesdylangoldstein-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Mar-28 02:53 UTC
Re: Total beginner asks a few questions
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 variables. 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 @. On Mar 27, 9:18 pm, Scott Mathieson <uberk...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> running idiot wrote: > > Hi there, > > > I am doing the "depot" application from Agile Web Development With > > Rails book - and there is a lot of copy-and-paste. And I have to say, > > as a beginner in both Ruby and Rails, I don''t understand quite a lot > > of things. > > > Let''s talk about this thing - there is a method called "add_to_cart". > > I don''t understand much, what it''s doing... (it''s in class > > StoreController < ApplicationController) > > > def add_to_cart > > product = Product.find(params[:id]) > > @cart = find_cart > > @cart.add_product(product) > > redirect_to(:action => ''display_cart'') > > end > > > So, I have a few questions. > > > 1) what exactly is product? Why it''s not @product? Is it global or > > local variable? > > product is a local variable used to store the result of Product.find(...)> 2) what exactly is Product? I''m not sure, but I think its the only > > instance of Product class, right? Then why is the name of instance and > > name of the class the same? > > Product.find(...) is calling the ''Product'' _class_ method ''find'' - it''s > not an instance > > > 3) What exactly is this params thing? > > params is the parameter hash passed when the request is made, e.g. when > you submit a form your data comes in through params - ditto when you > make a GET request via an URL (/user/23 for example)> 4) what exactly is :id? I don''t understand, in which cases you use > > colon and in which not - why I have to use it in Product.find, but not > > in add_product? > > the preceding colon signifies a symbol - there''s some good info on ruby > symbols here =>http://glu.ttono.us/articles/2005/08/19/understanding-ruby-symbols&& > here =>http://www.troubleshooters.com/codecorn/ruby/symbols.htm> 5) what exactly does => mean? Why I can''t use ordinary = ? > > key=>value, pretty common across different languages> Sorry if I ask too much, I just want to understand this language and > > this framework. > > perhaps have a look @http://pine.fm/LearnToProgram/- a tasty little > morsel that should help - not the most comprehensive answer but it''s 2am > and i''ve been dipping the whiskey :P > > > Kaja Bilek--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
jamesdylangoldstein-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> 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 > variables. > > 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, def add_to_cart product = Product.find(params[:id]) @cart = find_cart @cart.add_product(product) redirect_to(:action => ''display_cart'') end 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. Cheers Mohit. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
thecolonial-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Mar-28 08:18 UTC
Re: Total beginner asks a few questions
Hi Kaja, I''d just like to say that if you''re a total beginner, copying and pasting is not what you should be doing. The best thing to do is to manually type out the examples as you read them. Why? Because you WILL make mistakes, and by making them, you must learn how to fix them to continue. You''ll learn a lot more a lot faster if you do this. Skimming a book and pasting examples isn''t a good way to learn. Get your hands dirty! Making mistakes is a great way to learn :) Cheers! OJ On Mar 28, 10:33 am, "running idiot" <running...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi there, > > I am doing the "depot" application from Agile Web Development With > Rails book - and there is a lot of copy-and-paste. And I have to say, > as a beginner in both Ruby and Rails, I don''t understand quite a lot > of things. > > Let''s talk about this thing - there is a method called "add_to_cart". > I don''t understand much, what it''s doing... (it''s in class > StoreController < ApplicationController) > > def add_to_cart > product = Product.find(params[:id]) > @cart = find_cart > @cart.add_product(product) > redirect_to(:action => ''display_cart'') > end > > So, I have a few questions. > > 1) what exactly is product? Why it''s not @product? Is it global or > local variable? > 2) what exactly is Product? I''m not sure, but I think its the only > instance of Product class, right? Then why is the name of instance and > name of the class the same? > 3) What exactly is this params thing? > 4) what exactly is :id? I don''t understand, in which cases you use > colon and in which not - why I have to use it in Product.find, but not > in add_product? > 5) what exactly does => mean? Why I can''t use ordinary = ? > > Sorry if I ask too much, I just want to understand this language and > this framework. > > Kaja Bilek--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
jamesdylangoldstein-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Mar-28 13:09 UTC
Re: Total beginner asks a few questions
Ok, great post. The only other question I have, so I go to my Product class and find is inside a method. But I don''t think it''s refering to that. It''s a universal method that rails auto-creates? On Mar 27, 11:05 pm, Mohit Sindhwani <mo_m...-RxrYI66vbj0AvxtiuMwx3w@public.gmane.org> wrote:> jamesdylangoldst...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote: > > 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 > > variables. > > > 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, > > def add_to_cart > product = Product.find(params[:id]) > @cart = find_cart > @cart.add_product(product) > redirect_to(:action => ''display_cart'') > end > > 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. > Cheers > Mohit.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
jamesdylangoldstein-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org wrote:> Ok, great post. The only other question I have, so I go to my Product > class and find is inside a method. But I don''t think it''s refering to > that. It''s a universal method that rails auto-creates? > >Thanks. Find is a part of the ActiveRecord class. Note that your Product class inherits from the ActiveRecord (remember the first line of the model file was: class Product < ActiveRecord::Base ActiveRecord does the Object-Relational mapping (ORM) from a database row to the object. Check out: http://ar.rubyonrails.com/classes/ActiveRecord/Base.html#M000344 (not just the anchor, the whole page) for much more information about ActiveRecord. Cheers Mohit. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ok so... in your view on your add to cart button you have this code :action => :add_to_cart, :id => product, this calls the add_to_cart method and the id it gives is the products id. Now in the add_to_cart Product.find(params[:id]), first the params[:id] gets the id of the product you submited because of the code on your add to cart button in view. so if you submited product with id=1 your Product.find will be Product.find(1) and so on, this params[:id] is to see what id to put in . If you still dont understand ill try to explain it more. So this code searches for product with id you submited. Then you have @cart = find_cart this makes a variable with your session because in your find_cart method you created a new session that stores your products in cart Then @cart.add_product(product) this calls the add_to_product method in cart.rb. Btw. I had the same problems when i was reading that part...Btw. youll probably have problems with cartitem later to figure out how thigs work... I hope i helped a little. P.S Sorry for my bad english... On Mar 28, 2:33 am, "running idiot" <running...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi there, > > I am doing the "depot" application from Agile Web Development With > Rails book - and there is a lot of copy-and-paste. And I have to say, > as a beginner in both Ruby and Rails, I don''t understand quite a lot > of things. > > Let''s talk about this thing - there is a method called "add_to_cart". > I don''t understand much, what it''s doing... (it''s in class > StoreController < ApplicationController) > > def add_to_cart > product = Product.find(params[:id]) > @cart = find_cart > @cart.add_product(product) > redirect_to(:action => ''display_cart'') > end > > So, I have a few questions. > > 1) what exactly is product? Why it''s not @product? Is it global or > local variable? > 2) what exactly is Product? I''m not sure, but I think its the only > instance of Product class, right? Then why is the name of instance and > name of the class the same? > 3) What exactly is this params thing? > 4) what exactly is :id? I don''t understand, in which cases you use > colon and in which not - why I have to use it in Product.find, but not > in add_product? > 5) what exactly does => mean? Why I can''t use ordinary = ? > > Sorry if I ask too much, I just want to understand this language and > this framework. > > Kaja Bilek--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---