Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do i use a ruby if statement in a js.erb file?

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

("$('#cart').show(2000);") if @cart.total_items ==1
like image 869
Pete_ch Avatar asked Aug 13 '12 04:08

Pete_ch


2 Answers

<% if @cart.total_items ==1 %>
  $('#cart').show(2000);
<% end %>
like image 109
xdazz Avatar answered Oct 23 '22 14:10

xdazz


@xdazz answer is right , but i used to use the conditional statements as follows

var mycount = <%= @cart.total_items %>

if (mycount == 1){
$("#cart").show(2000)
}

The reason i use mostly conditional statements in javascript (in js.erb file) is because this seems easy to handle

like image 37
Mani Avatar answered Oct 23 '22 15:10

Mani