This is the error I am getting while trying out the shopping cart example in the beta book: SyntaxError in <controller not set>#<action not set> /usr/local/lib/ruby/gems/1.8/gems/activesupport-1.0.4/lib/active_support/dependencies.rb:189:in `load'': app/controllers/store_controller.rb:46: syntax error (eval):1:in `eat_path_to_controller'' script/server:48 this is the code that seems to be giving this error: def checkout @items = cart.items if @items.empty? redirect_to_index("There is nothing in the cart") else @order = Order.new @page_title = "Checkout" end wonder what am I doing wrong, any pointers please Thanks a bunch Hariom
On 6/7/05, Hariharan Gopalan <hariom100-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> This is the error I am getting while trying out the shopping cart > example in the beta book: > > SyntaxError in <controller not set>#<action not set> > > /usr/local/lib/ruby/gems/1.8/gems/activesupport-1.0.4/lib/active_support/dependencies.rb:189:in > `load'': app/controllers/store_controller.rb:46: syntax error > > (eval):1:in `eat_path_to_controller'' > script/server:48 > > this is the code that seems to be giving this error: > > def checkout > @items = cart.itemsWhat''s ''cart''?
On Tue, 2005-06-07 at 17:23 -0400, Hariharan Gopalan wrote:> This is the error I am getting while trying out the shopping cart > example in the beta book: > > SyntaxError in <controller not set>#<action not set>> def checkout > @items = cart.items > if @items.empty? > redirect_to_index("There is nothing in the cart") > else > @order = Order.new > @page_title = "Checkout" > endthis ''end'' closes your ''if''. Did you close youre checkout method with one more ''end''?
> def checkout> @items = cart.items I changed cart.items to session[:cart].items and all seems ok! I''ve attached my store controller with all the changes. _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On 6/7/05, Robert Williams <booshank-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > def checkout > > @items = cart.items > > I changed cart.items to session[:cart].items and all seems ok! I''ve > attached my store controller with all the changes. >I figured that the problem was with cart. The book''s code should work fine as is though. Perhaps you missed something earlier that set the cart variable?
On 08/06/05, Joe Van Dyk <joevandyk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > On 6/7/05, Robert Williams <booshank-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > def checkout > > > @items = cart.items > > > > I changed cart.items to session[:cart].items and all seems ok! I''ve > > attached my store controller with all the changes. > > > > I figured that the problem was with cart. > > The book''s code should work fine as is though. Perhaps you missed > something earlier that set the cart variable?I forgot to add the following code from page 74: private def cart @cart ||= session[:cart] unless @cart @cart = Cart.new session[:cart] = @cart end @cart end Oops! _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
> >>> def checkout >>> @items = cart.items >> >> I changed cart.items to session[:cart].items and all seems ok! I''ve >> attached my store controller with all the changes. > > I figured that the problem was with cart. > > The book''s code should work fine as is though. Perhaps you missed > something earlier that set the cart variable?To those not following along with book at hand, "cart" in this controller is an accessor method that returns, surprise, a user''s cart. It *may* be session[:cart], but of course, if that variable doesn''t exist, it won''t be. The accessor method handles that case. To the original poster: make sure that the last line of the cart method is just the variable "@cart". This means to return the cart to the calling method, in this case checkout. The cart method in full: def cart @cart ||= session[:cart] unless @cart @cart = Cart.new session[:cart] = @cart end @cart # i.e. return value of @cart to caller end
In article <1A92CABB-0426-4A9F-8D03-F4EFDF89A75A-uQjPo4GTFqgS+FvcfC7Uqw@public.gmane.org>, andrew- uQjPo4GTFqgS+FvcfC7Uqw-XMD5yJDbdMReXY1tMh2IBg@public.gmane.org says...> To those not following along with book at hand, "cart" in this > controller is an accessor method that returns, surprise, a user''s > cart. It *may* be session[:cart], but of course, if that variable > doesn''t exist, it won''t be. The accessor method handles that case.Speaking of this: When I was reading the book yesterday, it occurred to me that this also creates a "cart" *action*, right? Isn''t that a bad idea from a security perspective, to create a private-style object that''s actually public to the end user? I notice they don''t address it in the following "handle errors" chapter, but not sure if it''s touched upon later. I''m a Ruby newby, so maybe I''m missing something, and I haven''t tried the code yet. Jay -- Jay Levitt | Wellesley, MA | I feel calm. I feel ready. I can only Faster: jay at jay dot fm | conclude that''s because I don''t have a http://www.jay.fm | full grasp of the situation. - Mark Adler
> When I was reading the book yesterday, it occurred to me that this also > creates a "cart" *action*, right?Private methods aren''t created as actions! Thankfully :) Look in the middle of page 480. You''ll see "private" just above the comments. Rob. _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
> I forgot to add the following code from page 74: > > private > def cart > @cart ||= session[:cart] > unless @cart > @cart = Cart.new > session[:cart] = @cart > end > @cart > end > > Oops!or def cart @cart ||= session[:cart] ||= Cart.new end -- Tobi http://www.snowdevil.ca - Snowboards that don''t suck http://typo.leetsoft.com - Open source weblog engine http://blog.leetsoft.com - Technical weblog
On Jun 8, 2005, at 9:45 AM, Tobias Luetke wrote:> def cart > @cart ||= session[:cart] ||= Cart.new > end >Yeah - I had something like that, but backed away as I thought it was just a tad scary for Ruby Nubies.
In article <ca3194180506080649f292040-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>, booshank- Re5JQEeQqe8AvxtiuMwx3w-XMD5yJDbdMReXY1tMh2IBg@public.gmane.org says...> Private methods aren''t created as actions! Thankfully :) > > Look in the middle of page 480. You''ll see "private" just above the > comments.Ah! Thanks. I guess I would have discovered that once the "File" links start working. Phew. -- Jay Levitt | Wellesley, MA | I feel calm. I feel ready. I can only Faster: jay at jay dot fm | conclude that''s because I don''t have a http://www.jay.fm | full grasp of the situation. - Mark Adler
On 6/9/05, Dave Thomas <dave-kbbdpT5sCmpWk0Htik3J/w@public.gmane.org> wrote:> > On Jun 8, 2005, at 9:45 AM, Tobias Luetke wrote: > > def cart > > @cart ||= session[:cart] ||= Cart.new > > end > > > > Yeah - I had something like that, but backed away as I thought it was > just a tad scary for Ruby Nubies.It''s beautiful! Newbies will love it. Learning a new idiom always makes me want to weep with joy! :) Please include it in Beta3! -- Gavri http://livejournal.com/users/ga_woo
Jay Levitt wrote:> andrew-uQjPo4GTFqgS+FvcfC7Uqw-XMD5yJDbdMReXY1tMh2IBg@public.gmane.org says... > >>To those not following along with book at hand, "cart" in this >>controller is an accessor method that returns, surprise, a user''s >>cart. It *may* be session[:cart], but of course, if that variable >>doesn''t exist, it won''t be. The accessor method handles that case. > > Speaking of this: > > When I was reading the book yesterday, it occurred to me that this also > creates a "cart" *action*, right? Isn''t that a bad idea from a security > perspective, to create a private-style object that''s actually public to > the end user? I notice they don''t address it in the following "handle > errors" chapter, but not sure if it''s touched upon later. I''m a Ruby > newby, so maybe I''m missing something, and I haven''t tried the code yet.It''s covered in section 21.6: "Don''t Expose Controller Methods". Only public controller methods are exposed as actions, and the cart accessor is private. Justin Forder