I am trying to run the following lines in my Rails app in an erb file:
<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>
I am getting errors with the line containing <%=current_user.id%>, which seems to be analyzed even if the conditional is not satisfied:
Called id for nil, which would mistakenly be 8 -- if you really wanted the id of nil, use object_id
How do I handle a javascript conditional with a ruby variable?