Agile Web Developement with Rails

I recently got a copy of the second addition in PDF
and got stuck on the Ajax portion using highlighting
page 128. While trying to make my cart flash I got an
RJS error when I click add to cart. I have gone back
and made sure my code matched what was in the book but
no help. I sent Dave an email and he rec’d this list.
Anybody got any good links for troubleshooting Ajax or
maybe have see this book and have an idea about petty
problems I may have over looked?

I really hate to skip past this without finding out
where I went wrong.
TIA
Tom


Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around

What’s the error , maybe show the code here.
Also, did you copy the code from the book or download it from the site
? If you did not download, try that as well.

Stuart

On 7/18/06, Tom A. [email protected] wrote:

Anybody got any good links for troubleshooting Ajax

Firebug is good for debugging javascript:

http://www.joehewitt.com/software/firebug/

Tom A. wrote:

just spills it’s guts.

Great email list, gonna take me a few days just
reading some of these threads.

Thanks for all the feedback,
Tom

I’m sure I had this problem! But I can’t remember what the problem
was… I think that I had forgotten to change the file
“app/views/store/index.html” - the mistake I had made was that I did not
change it to have the “form_remote_tag”

Hope that helps. For what it’s worth, that example does work (it did
work for me) and I did not download the code from the net (so, the code
in the book is correct)… you’re probably missing something.

Cheers
mohit.

"Did you forget…

<%= _javascript_include_tag :defaults %>"

Nope first thing I checked and I first typed it, got a
problem then went and downloaded the files and got the
same error.

I haven’t tried the firefox, I will today.
Attached is a screenshot of the error, looks like it
just spills it’s guts.

Great email list, gonna take me a few days just
reading some of these threads.

Thanks for all the feedback,
Tom

Tom A. wrote:

The error is like this:

TypeError: $(“current_item”) has no properties

Is “current_item” in fact a DOM node with id “current_item”? Is it a
table row?

Just tried it with Firefox and actually got some info.
What I’m trying to do is make the items in the cart
flash when something is added. The error is like this:

TypeError: $(“current_item”) has no properties

So atleast I know that it has a problem with the
current_item. Here is my /model/cart.rb file:
#—

Excerpted from "Agile Web D. with Rails,

2nd Ed."

We make no guarantees that this code is fit for any

purpose.

Visit

http://www.pragmaticprogrammer.com/titles/rails2 for
more book information.
#—
class Cart

include Reloadable

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

def total_price
@items.inject(0) { |sum, item| sum + item.price }
end
end

and the RJS from my /views/store/add_to_cart.rjs:

page[:cart].replace_html :partial => ‘cart’, :object
=> @cart

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

So I’m confused it looks like I have defined
current_item correctly? or should it be in the
store_controller.rb?
Tom
— Tom A. [email protected] wrote:

I haven’t tried the firefox, I will today.


Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam
protection around
http://mail.yahoo.com >


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails


Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around

Did you forget…

<%= javascript_include_tag :defaults %>

in your layout, or the page. This is a gotcha that gets me every so
often :slight_smile:

~Jamie

I’m new to this so I’m trying to make sure I know what
I’m doing, at this point that’s very little:

"Is “current_item” in fact a DOM node with id

“current_item”? Is it a
table row?"
Is it a data object model and does it have an ID?
SOme of this is kinda spinning me until I’m dizzy but
according to the book:
I need to flag the current item so /models/cart.rb has
this:
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 << current_item
end
current_item
end

Next I need to pass that info down to the template so
my /controller/store_controller.rb has this in it:
def add_to_cart
begin
@product = Product.find(params[:id])
rescue
logger.error(“Attempt to access invalid product
#{params[:id]}”)
redirect_to_index(“Invalid product”)
else
@cart = find_cart

  @current_item = @cart.add_product(@product)

end

end

So product has an ID, then current_item is set the
product. So I’m not sure.

Tom

— Greg [email protected] wrote:


Posted via http://www.ruby-forum.com/.


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails


Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around

I’m having exactly the same problem, did this get resolved?

The problem is that you’ve got @current_item defined as an object in the
controller, and you’re trying to reference it as an item in the page.

The line @current_item = @cart.add_product(@product) is returning a
reference
to the database information about the item you’re adding to your cart.

Then, when you do page[:current_item], that is trying to find something
in the html of your page that is called “current_item”… it’s
referencing the database object that you have stored as @current_item.

What you need to do is figure out some way to have your partial that is
rendering the cart tag the item you just added with “id=‘current_item’”.
I’m not sure how that partial is rendering your cart, so I couldn’t tell
you for sure how to go about doing that.

Tom A. wrote:

I’m new to this so I’m trying to make sure I know what
I’m doing, at this point that’s very little:

"Is “current_item” in fact a DOM node with id

“current_item”? Is it a
table row?"
Is it a data object model and does it have an ID?
SOme of this is kinda spinning me until I’m dizzy but
according to the book:
I need to flag the current item so /models/cart.rb has
this:
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 << current_item
end
current_item
end

Next I need to pass that info down to the template so
my /controller/store_controller.rb has this in it:
def add_to_cart
begin
@product = Product.find(params[:id])
rescue
logger.error(“Attempt to access invalid product
#{params[:id]}”)
redirect_to_index(“Invalid product”)
else
@cart = find_cart

  @current_item = @cart.add_product(@product)

end

end

So product has an ID, then current_item is set the
product. So I’m not sure.

Tom

— Greg [email protected] wrote:


Posted via http://www.ruby-forum.com/.


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails


Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com

So it is the partial that is not picking up ‘current_item’? Here’s the
partial:

<% if cart_item == @current_item %>

<% else %> <% end %> <%= cart_item.quantity %>× <%= h(cart_item.title) %> <%= number_to_currency(cart_item.price) %>

it’s called_cart_item.rhtml

It looks ok to me but I’m new to partials and ruby on rails.

Doh, I typoed, please read this one instead:
The line @current_item = @cart.add_product(@product) is returning a
reference to the database information about the item you’re adding to
your cart.

Then, when you do page[:current_item], that is trying to find something
in the html of your page that is called “current_item”… it’s NOT(<-
missed this the first time, it’s kinda important) referencing the
database object that you have stored as @current_item.

What you need to do is figure out some way to have your partial that is
rendering the cart tag the item you just added with “id=‘current_item’”.
I’m not sure how that partial is rendering your cart, so I couldn’t tell
you for sure how to go about doing that.

I am having the same problem. I figured out @current_item
is not equal to ‘cart_item’. So, if you set the ‘if’ block
like below, you can see the effects working,

<% if cart_item == @current_item %>

<% else %> <% end %>

But, my main concern is, why ‘cart_item == @current_item’ is false.
Anyone solved this case…?

-Arul

james seavers wrote:

So it is the partial that is not picking up ‘current_item’? Here’s the
partial:

<% if cart_item == @current_item %>

<% else %> <% end %> <%= cart_item.quantity %>× <%= h(cart_item.title) %> <%= number_to_currency(cart_item.price) %>

it’s called_cart_item.rhtml

It looks ok to me but I’m new to partials and ruby on rails.

Arulchandran P wrote:

I am having the same problem. I figured out @current_item
is not equal to ‘cart_item’. So, if you set the ‘if’ block
like below, you can see the effects working,

<% if cart_item == @current_item %>

<% else %> <% end %>

But, my main concern is, why ‘cart_item == @current_item’ is false.
Anyone solved this case…?

-Arul

Could you check your Cart model? Be sure that add_product function looks
like

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

and not:
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
end

It’s possible that you’re returning nil.

Wow, I’ve got it sorted. $(current_item) had no properties because the
store controller wasn’t passing it any because I had typed the code
wrong:

@current_item == @cart.add_product(product)

should have been:

@current_item = @cart.add_product(product)

@current_item could not have had any properties unles it was equal to
@cart.add_product(product). It should just be the same as
@cart.add_product(product).

(if that makes sense!)

Thanks Luke, you got me thinking as to why there was no properties,
thanks again for your help.