Hello ! I have bought book Agile web development with Ruby on Rails and i have come to the section: sessions. I am having some problems figuring out what does each line of code do. So i would be gratefull if somoeone would explain it to me. First I have this: session[:cart] ||= Cart.new Ok i understand the second part which creates new if there isnt an existing one. but the firt part is what i dont understand. session[:cart] :cart is because of the name of the model cart.rb ? Can i add more models ? Something like session[:cart, :person] ? One more thing i dont understand is this line product=Product.find(params[:id]) in add_to_cart function. I understand in general that it finds the id but where can i find more documentation about params and how it is used ? I have a link http://api.rubyonrails.org/ but it seems to me like it isnt full ? I check for params cant find it, check for session something small and unusuable... Please help me ! I want to learn but im stuck :( --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Hi Eric, Product.find(params[:id]) selects the record whose primary key value matches the id passed in the parameters. So, if your url looks like http://railsapp/product/show/1, params[:id] would be 1, and the corresponding record gets selected. The symbol, :cart isn''t used because it matches the name of the model. You can use any name for the session variables and can also save multiple values in a session. Chaitanya On Mar 10, 5:20 pm, "Eric" <jardas.e...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hello ! > I have bought book Agile web development with Ruby on Rails and i have > come to the section: sessions. > > I am having some problems figuring out what does each line of code do. > So i would be gratefull if somoeone would explain it to me. > > First I have this: > > session[:cart] ||= Cart.new > > Ok i understand the second part which creates new if there isnt an > existing one. but the firt part is what i dont understand. > > session[:cart] > > :cart is because of the name of the model cart.rb ? > Can i add more models ? Something like session[:cart, :person] ? > > One more thing i dont understand is this line > > product=Product.find(params[:id]) > > in add_to_cart function. > I understand in general that it finds the id but where can i find more > documentation about params and how it is used ? > > I have a linkhttp://api.rubyonrails.org/but it seems to me like it > isnt full ? I check for params cant find it, check for session > something small and unusuable... > > Please help me ! I want to learn but im stuck :(--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Thank you very much fr your fast reply. Could you tell me the correct syntax for saving multiple values to a session ? On Mar 10, 1:38 pm, "askme" <ChaitanyaPrakas...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi Eric, > > Product.find(params[:id]) selects the record whose primary key value > matches the id passed in the parameters. > > So, if your url looks likehttp://railsapp/product/show/1, > params[:id] would be 1, and the corresponding record gets selected. > > The symbol, :cart isn''t used because it matches the name of the > model. You can use any name for the session variables and can also > save multiple values in a session. > > Chaitanya > > On Mar 10, 5:20 pm, "Eric" <jardas.e...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Hello ! > > I have bought book Agile web development with Ruby on Rails and i have > > come to the section: sessions. > > > I am having some problems figuring out what does each line of code do. > > So i would be gratefull if somoeone would explain it to me. > > > First I have this: > > > session[:cart] ||= Cart.new > > > Ok i understand the second part which creates new if there isnt an > > existing one. but the firt part is what i dont understand. > > > session[:cart] > > > :cart is because of the name of the model cart.rb ? > > Can i add more models ? Something like session[:cart, :person] ? > > > One more thing i dont understand is this line > > > product=Product.find(params[:id]) > > > in add_to_cart function. > > I understand in general that it finds the id but where can i find more > > documentation about params and how it is used ? > > > I have a linkhttp://api.rubyonrails.org/butit seems to me like it > > isnt full ? I check for params cant find it, check for session > > something small and unusuable... > > > Please help me ! I want to learn but im stuck :(--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Though, I''ve used sessions till now, I''m confident that you could do something like this.. session[:user] = User.find(''eric'') session[:cart] = Cart.find_and_add(''item1'') - Chaitanya On Mar 10, 6:05 pm, "Eric" <jardas.e...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Thank you very much fr your fast reply. > Could you tell me the correct syntax for saving multiple values to a > session ? > > On Mar 10, 1:38 pm, "askme" <ChaitanyaPrakas...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > Hi Eric, > > > Product.find(params[:id]) selects the record whose primary key value > > matches the id passed in the parameters. > > > So, if your url looks likehttp://railsapp/product/show/1, > > params[:id] would be 1, and the corresponding record gets selected. > > > The symbol, :cart isn''t used because it matches the name of the > > model. You can use any name for the session variables and can also > > save multiple values in a session. > > > Chaitanya > > > On Mar 10, 5:20 pm, "Eric" <jardas.e...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Hello ! > > > I have bought book Agile web development with Ruby on Rails and i have > > > come to the section: sessions. > > > > I am having some problems figuring out what does each line of code do. > > > So i would be gratefull if somoeone would explain it to me. > > > > First I have this: > > > > session[:cart] ||= Cart.new > > > > Ok i understand the second part which creates new if there isnt an > > > existing one. but the firt part is what i dont understand. > > > > session[:cart] > > > > :cart is because of the name of the model cart.rb ? > > > Can i add more models ? Something like session[:cart, :person] ? > > > > One more thing i dont understand is this line > > > > product=Product.find(params[:id]) > > > > in add_to_cart function. > > > I understand in general that it finds the id but where can i find more > > > documentation about params and how it is used ? > > > > I have a linkhttp://api.rubyonrails.org/butitseems to me like it > > > isnt full ? I check for params cant find it, check for session > > > something small and unusuable... > > > > Please help me ! I want to learn but im stuck :(- Hide quoted text - > > - Show quoted text ---~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Do you know where can i check this ? I havent find any good documentation on session :( On Mar 10, 2:15 pm, "askme" <ChaitanyaPrakas...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Though, I''ve used sessions till now, I''m confident that you could do > something like this.. > > session[:user] = User.find(''eric'') > session[:cart] = Cart.find_and_add(''item1'') > > - Chaitanya > On Mar 10, 6:05 pm, "Eric" <jardas.e...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Thank you very much fr your fast reply. > > Could you tell me the correct syntax for saving multiple values to a > > session ? > > > On Mar 10, 1:38 pm, "askme" <ChaitanyaPrakas...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Hi Eric, > > > > Product.find(params[:id]) selects the record whose primary key value > > > matches the id passed in the parameters. > > > > So, if your url looks likehttp://railsapp/product/show/1, > > > params[:id] would be 1, and the corresponding record gets selected. > > > > The symbol, :cart isn''t used because it matches the name of the > > > model. You can use any name for the session variables and can also > > > save multiple values in a session. > > > > Chaitanya > > > > On Mar 10, 5:20 pm, "Eric" <jardas.e...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > Hello ! > > > > I have bought book Agile web development with Ruby on Rails and i have > > > > come to the section: sessions. > > > > > I am having some problems figuring out what does each line of code do. > > > > So i would be gratefull if somoeone would explain it to me. > > > > > First I have this: > > > > > session[:cart] ||= Cart.new > > > > > Ok i understand the second part which creates new if there isnt an > > > > existing one. but the firt part is what i dont understand. > > > > > session[:cart] > > > > > :cart is because of the name of the model cart.rb ? > > > > Can i add more models ? Something like session[:cart, :person] ? > > > > > One more thing i dont understand is this line > > > > > product=Product.find(params[:id]) > > > > > in add_to_cart function. > > > > I understand in general that it finds the id but where can i find more > > > > documentation about params and how it is used ? > > > > > I have a linkhttp://api.rubyonrails.org/butitseemsto me like it > > > > isnt full ? I check for params cant find it, check for session > > > > something small and unusuable... > > > > > Please help me ! I want to learn but im stuck :(- Hide quoted text - > > > - Show quoted text ---~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
It is not a good practice to save objects into the session for several reasons. One is that you are unnecessarily impacting the performance, you also will run into problems when you have changed your model but the sessions consists of the older version models, which is very difficult to debug. So instead of storing the object you can store the id of the object in the session. On 3/10/07, askme <ChaitanyaPrakash.N-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > Though, I''ve used sessions till now, I''m confident that you could do > something like this.. > > session[:user] = User.find(''eric'') > session[:cart] = Cart.find_and_add(''item1'') > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
http://api.rubyonrails.com/classes/ActionController/SessionManagement/ClassMethods.html describes the available session methods. On 3/10/07, Eric <jardas.eric-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > Do you know where can i check this ? > I havent find any good documentation on session :( > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Eric wrote:> Do you know where can i check this ? > I havent find any good documentation on session :(Eric, While some of the documentation says it is only similar, in general use you can think of the session as a simple Hash object. With any hash object, you can add stuff to it for a new key by simply doing something like: my_hash = hash.New my_hash[:foo] = "bar" my_hash[:everything] = 42 same is true for the "session hash" session = hash.New session[:foo] = "bar" session[:everything] = 42 session[:my_blog_entry] = this_blog_entry.id You can make these keys up at will and add them as above. The usefulness of the session hash is that stuff you put in there is "remembered" from one action to another and also from one controller to another. Wherever your user goes in the app, his session data goes with him. Because of the way http works, this is not true for a simple global variable. So, whenever you need to "remember" something outside the boundaries of a single action in a single controller, you need to save it (or a reference to it, such as an "id" that can be found later) in the session "hash" as shown above. hth, jp -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Jeff Pritchard wrote:> > same is true for the "session hash" > session = hash.New > session[:foo] = "bar"oops, stupid mistake. You don''t create a new session hash with hash.new, you just use the session object created by rails. I copied that and pasted it below and forgot to remove that line. jp -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---