How to insert conditional in a rjs file

Hi,

I’m following the book Agile Web D. with Rails and I have
finished to implement the action “remove_from_cart” which is one of the
task in the Playtime section of page 139.

Everything works well; I highlight the current_item which has been
modify, and I even subtract the item from the cart when the quantity is
= 0. However the highlight trick give an error when I extract the last
item, and it make sense because there is note current_item any more.

How can I make this effect happen only when quantity is > 0?

Here I leave what I’m doing but it’s not working.

remove_from_cart.js
—-—-—-——---------------
page.replace_html(“cart” , :partial => “cart” , :object => @cart)

page[:cart].visual_effect :blind_up if @cart.total_items <= 0

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

Thanks in advance

Whenever you want to do something with an element that may not be
present on the page using the select method makes a lot of sense. It
builds a collection of all the elements that can be described by some
css select (hence id’s or css classes) and then you can use something
like .each to iterate through the collection. Something like:

page.select(’#current_item’).each{|elem|
elem.visual_effect :highlight, :startcolor => “#88ff88”, :endcolor =>
#114411

On Nov 28, 11:54 am, Alberto Trujillo <rails-mailing-l…@andreas-

something like this?

page << “if ($(‘current_item’)) {”
page[:current_item].visual_effect :highlight
page << “}”

On Nov 28, 10:54 am, Alberto Trujillo <rails-mailing-l…@andreas-

Thank you guys,

Both options worked fine :slight_smile:

Andy: There is a missing “}” at the end of your line of code. I just
mention it in case somebody else try to use it.