ERB Javascript条件错误

ERB Javascript条件错误,javascript,ruby-on-rails,ruby,Javascript,Ruby On Rails,Ruby,我正在尝试在我的Rails应用程序的erb文件中运行以下行: <script> if("<%=current_user%>".length>0){ console.log('set mixpanel current user'); mixpanel.identity("<%=current_user.id%>"); }else{ console.log('set mixpanel identity to 0');

我正在尝试在我的Rails应用程序的erb文件中运行以下行:

<script>
  if("<%=current_user%>".length>0){
    console.log('set mixpanel current user');
    mixpanel.identity("<%=current_user.id%>");
  }else{
    console.log('set mixpanel identity to 0');
    mixpanel.identify('0');
  }
</script>

如何使用ruby变量处理javascript条件?

在erb文件中,我们可以使用scriptlet嵌入ruby代码:

这应该起作用:

<script>
  <% if current_user %>
    console.log('set mixpanel current user');
    mixpanel.identity("<%=current_user.id%>");
  <% else %>
    console.log('set mixpanel identity to 0');
    mixpanel.identify('0');
  <% end %>
</script>

log('set mixpanel current user');
mixpanel.identity(“”);
log('set mixpanel identity to 0');
mixpanel.identified('0');

在erb文件中,我们可以使用scriptlet嵌入ruby代码:

这应该起作用:

<script>
  <% if current_user %>
    console.log('set mixpanel current user');
    mixpanel.identity("<%=current_user.id%>");
  <% else %>
    console.log('set mixpanel identity to 0');
    mixpanel.identify('0');
  <% end %>
</script>

log('set mixpanel current user');
mixpanel.identity(“”);
log('set mixpanel identity to 0');
mixpanel.identified('0');

当前用户
而不是
”。我想长度>0
可以用。

当前用户
而不是
”。我想长度>0
可以用