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 -~----------~----~----~----~------~----~------~--~---
chris wrote:> Is there something silly that I am missing or...? > > Thanks for any help.The only odd thing that jumps out at me is your begin/rescue/else block. I have never seen an else in one of those. You can use something simpler for that block def add_to_cart @product = Product.find(params[:id]) @cart = find_cart @cart.add_product(@product) #calls ont he method below rescue logger.error(''An attempt to access an invalid product: #{params[:id]}'') redirect_to_index "Invalid product" end Any error will cause an immediate jump to the rescue clause of the method. Although, is the product getting properly added to the cart? If so the instance variable is properly populated and should be available in the view. Is the variable visible outside the partial? If so you can pass it in via the :locals hash when you render the partial. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
As for the try/catch(rescue) I too found it a little odd since I have never done that before, but it does make sense in that if you catch the exception you know exactly where it is from. As for the program everything else works fine, in that the products are added to the cart and the quantities are updated if something is added that is already in the cart. In the example the check of the current product was done to add the proper tag and then dazzle it up with some scriptaculous. I tried to access the @product var in the main rhtml file and I don''t get anything there either <h1>The currently added product is: <%= @product %></h1> I also added a check within the add_to_cart method (and revised the try/rescue def add_to_cart begin @product = Product.find(params[:id]) @cart = find_cart @cart.add_product(@product) redirect_to_index "Something is missing here" if @product.nil? || @product == '''' rescue logger.error(''An attempt to access an invalid product: #{params[:id]}'') redirect_to_index "Invalid product" end end ---------- and it never redirects to the index page. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---