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

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

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

<!-- <%= @current_item %> -->
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.