Lee McKusick
2006-Aug-24 00:07 UTC
[Rails] Re: Not sure why this doesn''t work - AWDRW chapter 8 page 96 tutorial
First error I see is add parentheses around "Invalid Product" The code appears on the book page 96, 2nd edition AWDWR I think putting "AWDWR" and chapter and page number will help others searching the On Wed, 2006-08-23 at 02:51 +0000, chris wrote:> I really do hate posting these kind of questions, but it has been at > least an hour and a half and I can''t figure out why this isn''t working. > I am working on a tutorial in the Agile Development... book and I a > variable that I am trying to return to the page is empty for some > reason. Take a look > ------------- > > def add_to_cart > begin > @product = Product.find(params[:id]) > rescue > logger.error(''An attempt to access an invalid product: > #{params[:id]}'') > redirect_to_index "Invalid product" > else > @cart = find_cart > @cart.add_product(@product) #calls ont he method below > end > > end > =======> def add_product(product) > #determine if the product already exists within the cart > existing_item = @items.find {|item| item.product == product } > if existing_item > existing_item.increment_quantity > else > existing_item = CartItem.new(product) > @items << existing_item > end > end > > ======> in the _cart_item.rhtml file > ======> <% if cart_item.product == @product %> > <tr id="current_item"> > <% else %> > <!-- This is for debugging only --> > <!-- <%= cart_item.product %> --> > <!-- <%= @product %> --><!-- Nothing is shown for this value --> > <tr> > <% end %> > <td><%= cart_item.product.title %></td> > <td>× <%= cart_item.quantity %></td> > <td><%= number_to_currency(cart_item.product.price * > cart_item.quantity) %></td> > </tr> > > ===> Here is the class for the cart_item within the _cart_item.rthml file > above > ===> class CartItem > include Reloadable > > attr_reader :product, :quantity > > #create the cart item and add the product > def initialize(product) > @product = product > @quantity = 1 > end > > def increment_quantity > @quantity += 1 > end > > def title > @title > end > > def price > @product.price * @quantity > end > > end > > ------------------------ > In the _cart_item.rhtml file the main problem is that the <tr id="..."> > never renders. So I added the code within the comment lines (<!-- <%> @product %> -->) to get this: > <!-- #<Product:0x39ede80> --> > <!-- --> > Now that explains why the tr tag does not get id''d, but I can''t figure > out why nothing is being returned. The example in the book compares > the cart rather than the product, but I had the same problem with it as > well, and I thought that it made more sense to compare the product > rather than the cart item. > > Is there something silly that I am missing or...? > > Thanks for any help. > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---