I have 3 methods: def add_to_cart # do some stuff redirect_to_index end def empty_cart # do some stuff redirect_to_index("your cart was emptied") end def redirect_to_index(msg = nil) flash[:notice] = msg if msg redirect_to :action => :index end How come the flash notice works if I''m setting msg = nil? For example, if I''m in empty_cart and pass "your cart was emptied" into redirect_to_index, it passes through OK. I thought that setting something equal to nil would remove the value. I''m confused. Thanks! --~--~---------~--~----~------------~-------~--~----~ 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 -- On Fri, 25 Apr 2008, RoRNoob wrote:> > I have 3 methods: > > def add_to_cart > # do some stuff > redirect_to_index > end > > def empty_cart > # do some stuff > redirect_to_index("your cart was emptied") > end > > def redirect_to_index(msg = nil) > flash[:notice] = msg if msg > redirect_to :action => :index > end > > How come the flash notice works if I''m setting msg = nil? For example, > if I''m in empty_cart and pass "your cart was emptied" into > redirect_to_index, it passes through OK. I thought that setting > something equal to nil would remove the value. I''m confused.What you''ve got there is a default value; it means: Set msg to nil if and only if there''s no argument supplied when the method is called. Here''s an example: def say_word(word = "hello") puts "#{word}!" end say_word # word will be "hello" (the default value) say_word("goodbye") # word will be "goodbye" David -- Rails training from David A. Black and Ruby Power and Light: INTRO TO RAILS June 9-12 Berlin ADVANCING WITH RAILS June 16-19 Berlin INTRO TO RAILS June 24-27 London (Skills Matter) See http://www.rubypal.com for details and updates! --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
AHHHH - OK thanks! I''m going through ADWR 3 beta and it doesn''t explain that piece. On Apr 25, 5:03 pm, "David A. Black" <dbl...-0o/XNnkTkwhBDgjK7y7TUQ@public.gmane.org> wrote:> Hi -- > > > > On Fri, 25 Apr 2008,RoRNoobwrote: > > > I have 3 methods: > > > def add_to_cart > > # do some stuff > > redirect_to_index > > end > > > def empty_cart > > # do some stuff > > redirect_to_index("your cart was emptied") > > end > > > def redirect_to_index(msg = nil) > > flash[:notice] = msg if msg > > redirect_to :action => :index > > end > > > How come the flash notice works if I''m setting msg = nil? For example, > > if I''m in empty_cart and pass "your cart was emptied" into > > redirect_to_index, it passes through OK. I thought that setting > > something equal to nil would remove the value. I''m confused. > > What you''ve got there is a default value; it means: Set msg to nil if > and only if there''s no argument supplied when the method is called. > > Here''s an example: > > def say_word(word = "hello") > puts "#{word}!" > end > > say_word # word will be "hello" (the default value) > say_word("goodbye") # word will be "goodbye" > > David > > -- > Rails training from David A. Black and Ruby Power and Light: > INTRO TO RAILS June 9-12 Berlin > ADVANCING WITH RAILS June 16-19 Berlin > INTRO TO RAILS June 24-27 London (Skills Matter) > Seehttp://www.rubypal.comfor details and updates!--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---