Not sure why this doesn't work

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 %>


<% else %>




<% end %>
<%= cart_item.product.title %>
× <%= cart_item.quantity %>
<%= number_to_currency(cart_item.product.price *
cart_item.quantity) %>

====
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


never renders. So I added the code within the comment lines () to get this:


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.

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.

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

The currently added product is: <%= @product %>

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.

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

I will mark the page and book name in future posts. Thanks for the
tip.

As for the problem I am still unable to figure it out. I reverted to
exactly the way the book did things (or at least I think it is exact)
with no luck.

I think the issue is in the return of a value from the add_to_cart
method. In addition to trying to return the item and product, I have
also tried to return a simple string value ,but still don’t see any
value in the code within the comment block

after returning a value from the model:

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
return “It is added” #tried without the return keyword as well
end

and the controller:
@current_item = @cart.add_product(@product)

I finally got it to work. There wasn’t anything that got changed
besides clearing the session and restarting the server. There must
have been something that needed to be changed when the session was
first created??

All I know is that it is time to move on to the next page.

BTW it Dave is out there, hats off to you this is a very well written
book.