How can I use ruby ​​if statement in js.erb file?

I need to do something like the one shown below; add jquery effect depending on the value of the instance object in rails

("$('#cart').show(2000);") if @cart.total_items ==1 
+4
source share
2 answers
 <% if @cart.total_items ==1 %> $('#cart').show(2000); <% end %> 
+11
source

@xdazz answer is right, but I used conditional expressions as follows

 var mycount = <%= @cart.total_items %> if (mycount == 1){ $("#cart").show(2000) } 

The reason I use mostly conditional statements in javascript (in the js.erb file) is because it is easy to handle

+1
source

All Articles