我如何在js.erb文件中使用ruby if语句?

时间:2022-11-07 08:47:30

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

我需要做一些如下所示的事情;根据rails中实例对象的值添加jquery效果

("$('#cart').show(2000);") if @cart.total_items ==1

2 个解决方案

#1


12  

<% if @cart.total_items ==1 %>
  $('#cart').show(2000);
<% end %>

#2


1  

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

@xdazz答案是对的,但我习惯使用条件语句如下

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

我在javascript(在js.erb文件中)主要使用条件语句的原因是因为这似乎很容易处理

#1


12  

<% if @cart.total_items ==1 %>
  $('#cart').show(2000);
<% end %>

#2


1  

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

@xdazz答案是对的,但我习惯使用条件语句如下

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

我在javascript(在js.erb文件中)主要使用条件语句的原因是因为这似乎很容易处理