On Sep 7, 2009, at 5:30 PM, AlwaysCharging wrote: > > I'm programming my way through the Agile Web dev book (3rd) and > there's a part in there that's causing a bit of confusion (p. 146 to > be exact). > > You're creating a button in a partial (named _cart.html.erb) using the > following code: > > <%= button_to "Checkout" , :action => 'checkout' %> > > However, earlier in the book, we created a button to empty the cart > by: > > <%= button_to "Empty cart" , :action => :empty_cart %> > > both are pointing to actions defined in the store_controller.rb. > Everything's the same except that checkout is in single quotes and > empty_cart is preceded by a colon. Why is this? > > Is it that 'checkout' is passed a variable where empty_cart is not? > Or, am I having bigger brain fart than that? > > Thank you in advance.
:empty_cart is a symbol, whereas 'checkout' is a string. Your params hash will still wind up with :action => 'empty_cart' in either case, as the params hash is simply derived from parsing the query string. In this context, a string and a symbol are functionally equivalent because the symbol is converted to a string preparatory to sending the HTTP POST request. Using symbols as a kind of named constant is idiomatic when that object is immutable. So, empty_cart won't change names during your application's duration -- you can us a symbol instead of a string, thus keeping one and only one copy of it in memory. Use this as a background: http://en.wikipedia.org/wiki/String_interning I'm not sure whether this explanation helps or confuses :) --~--~---------~--~----~------------~-------~--~----~ 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 [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---

