# How to insert conditional in a rjs file

**URL:** <https://www.ruby-forum.com/t/how-to-insert-conditional-in-a-rjs-file/121522>\
**Category:** Rails\
**Created:** [November 28, 2007, 5:54pm UTC](https://www.ruby-forum.com/t/how-to-insert-conditional-in-a-rjs-file/121522 "2007-11-28T17:54:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![altrugon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/91b2a8/32.png) [@altrugon](https://www.ruby-forum.com/u/altrugon)\
**Post date:** [November 28, 2007, 5:54pm UTC](https://www.ruby-forum.com/t/how-to-insert-conditional-in-a-rjs-file/121522/1 "2007-11-28T17:54:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![altrugon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/91b2a8/32.png) [@altrugon](https://www.ruby-forum.com/u/altrugon)\
**Post date:** [November 28, 2007, 6:06pm UTC](https://www.ruby-forum.com/t/how-to-insert-conditional-in-a-rjs-file/121522/2 "2007-11-28T18:06:44Z")

</div>

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-

---

<div class="post-metadata">

**Author:** ![altrugon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/91b2a8/32.png) [@altrugon](https://www.ruby-forum.com/u/altrugon)\
**Post date:** [November 28, 2007, 6:18pm UTC](https://www.ruby-forum.com/t/how-to-insert-conditional-in-a-rjs-file/121522/3 "2007-11-28T18:18:27Z")

</div>

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-

---

<div class="post-metadata">

**Author:** ![altrugon](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/91b2a8/32.png) [@altrugon](https://www.ruby-forum.com/u/altrugon)\
**Post date:** [November 28, 2007, 6:29pm UTC](https://www.ruby-forum.com/t/how-to-insert-conditional-in-a-rjs-file/121522/4 "2007-11-28T18:29:58Z")

</div>

Thank you guys,

Both options worked fine 🙂

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.
