small medium large xlarge

Photo_2_pragsmall
10 Nov 2010, 12:02
Paul Nelligan (17 posts)

OK, I’ve followed the examples as per the book, I’ve checked and rechecked my code…

so I’m around about page 130, getting the AJAX part working… previously there had been no problem with the add_to_cart.html.erb file…

yet, when I introduce the partial in the layout:

`

<%= render(:partial => “cart”, :object => @cart) %>

`

which refers to _cart.html.erb: `

Your Cart</div>

<%= render(:partial => "cart_item" , :collection => cart.items) %> Total <%= number_to_currency(cart.total_price) %>

<%= button_to “Empty cart” , :action => :empty_cart %> `

I get an error saying that the total_price method is unrecognised:

undefined method total_price’ for # `

I found a workaround, and that is to define a variable called @total_price in the Cart class, and update it accordingly

But why is it not recognising the method, when it had been previously?… It seems the problem exists with the partial, and possibly the order in which everything was being interpreted… But i did play around with this quite a bit and still haven’t found a satisfactory answer…

Samr_small_pragsmall
19 Sep 2008, 13:22
Sam Ruby (634 posts)

Did you by any chance miss the step at the bottom of page 122 where the total_price method was added to “app/models/cart.rb”:http://media.pragprog.com/titles/rails3/code/depot_i/app/models/cart.rb ?

Photo_2_pragsmall
10 Nov 2010, 12:03
Paul Nelligan (17 posts)

thanks for your swift reply Sam

no, I most definitely didn’t miss this step…

the workaround I used was for the Cart class, which now works as:

` class Cart attr_reader :items, :total_price

def initialize @items = [] @total_price = 0; #added line end

def add_product(product) current_item = @items.find {|item| item.product == product} if current_item current_item.increment_quantity @total_price = @items.sum { |item| item.price } #added line else @items « CartItem.new(product) @total_price = @items.sum { |item| item.price } #added line end

def total_price @items.sum { |item| item.price } end

end end `

I even tried to rename the total price method, calling it from elsewhere in the class, and put the following code into the method, thereby obeying the DRY principle:

@total_price = @items.sum { |item| item.price }

and that wouldn’t work… for some reason the method is unrecognisable…

One more thing, I had a few installation problems initially on Ubuntu… something to do with replacing rubygem with rubygem1.8 in order to make it work… maybe this has affected something?… I can get the details if this is relevant…

Another issue that I had earlier which I’ve ignored is that the migration add_test_data wouldn’t work for me when I followed the instructions from the book…

I’m pretty sure I didn’t miss anything here, but you’d never know…

Samr_small_pragsmall
19 Sep 2008, 14:57
Sam Ruby (634 posts)

This forum software formatted your code badly (consider using <pre>), but given that there are three ends at the end, it almost looks like you added the total_price method inside your add_product method.

The way that Ruby works (not just Rails, but all of Ruby) is that:

“attr_reader :total_price” is equivalent to

def total_price
  return @total_price
end

If you later define a method named total_price, it will replace the previous definition. My only conclusion is that somehow the correct definition of total_price is not getting processed.

Photo_2_pragsmall
10 Nov 2010, 12:03
Paul Nelligan (17 posts)

Hi Sam

the mistake I made was that I confused ending the ‘if/else’ statement with ending the add_product method… therefore I never ended that method, and started the next one without closing it…

On the plus side, I learned more trying to find the solution than I ever would have if I had never made the mistake…

Also, thanks very much for your help

cheers

You must be logged in to comment