Monday, February 18, 2008

AWDR depot app for Rails 2.0 (RubyPlus.org) - Part 2

Here goes part 2.

I've finished the screencasts for chapter 8, 9, and 10.

Here are the problems I came across while doing so:

At the beginning of chapter 8, the top of the screen is cut off when creating sessions in the database, the is the whole line:

rake db:sessions:create


And then towards the beginning of chapter 10, a line is cut off in 005_create_line_items.rb

t.decimal :total_price, :null => false, :precision => 8, :scale => 2


I had some trouble with the ajax highlight effect in add_to_cart.js.rjs

page[:current_item].visual_effect :highlight,
:startcolor => "#88ff88",
:endcolor => "#114411"


and was not able to figure it out. When I clicked Add to Cart, I would get the error "TypeError: $("current_item") has no properties" and then another popup window with a lot of code in it. I looked around the internet for a while trying to get it fixed, but nothing I tried worked. If anyone figures this out, let me know. I ended up commenting out that line in add_to_cart.js.rjs.

Everything else works great. I really like Bala's screencasts, they are a great resource for learning Rails 2.0. After finishing them, I believe I know enough to make something that is not too complicated. For my next post, I believe I am going to start working on a forum in Rails, and AJAX it up(I could use a lot more experience with AJAX).

See you all next time!

5 comments:

Chris said...

I got the same error. I found out that I forgot to modify app/models/cart.rb

to what's on page 132 (AWDROR 2nd Ed)

My cart.rb was still like page 110.

@items << CartItem.new(product)

needs to modify to:

current_item = CartItem.new(product)
@items << current_item

otherwise the current_item returned is nil.


HTH

Reg said...

I made the same mistake as yours. The code transition from @items << CartItem.new(product) to @items << current_item was simply not apparent in the book

Mortimer Agustus Stanley said...

using 3rd edition, so will not cite pages.

My app/models/cart.rb is

class Cart

attr_reader :items

def initialize
@items = []
end

def add_product(product)
current_item = @items.find {|item| item.product == product}
if current_item
current_item.increment_quantity
else
current_item = CartItem.new(product)
@items << CartItem.new(product)
end
current_item || @items[0]
end

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

end


I'm seeing the same pair of error windows ... but only the 1st time a product is added to the cart. Add the same product to the cart a 2nd time, and all works as expected.

I haven't yet figured it out.

Thanks

Common Grounds said...

I found this same solution at the AWDR official website but it did not work for me.
I'm still getting RJS error: TypeError: $("current_item") is null

Here is my code for cart.rb, which is exactly the same as above:

def add_product(product)
current_item = @items.find {|item| item.product == product}
if current_item
current_item.increment_quantity
else
@items << CartItem.new(product)
end
current_item || @items[0]
end

kamagra said...

Thanks for this one, I was looking for this information for quite some time.