我希望根据用户的位置/页面触发一个AJAX操作。我构建了以下内容:
if (window.location.href.indexOf('users/stream') >= 0) {
alert("Condition 1 is met")
$('#invite_to_video').html("<%= j render partial: 'messages/contact_ir_video', :locals => {:message => Message.new} %>");
} else {
alert("Condition 2 is met")
$("#comments").html("<%= j render partial: '@message.comments' %>");
}
当我使用alert
简单地尝试if
条件时,它工作良好,并且显示条件1满足
。
但是,当我添加部分加载指令时,我会得到以下信息:
ActionView::Template::Error(缺少部分消息/@message.comments,Application/@message.comments,{:locale=>[:en],:formats=>[:js,:html],:variants=>[],:handlers=>[:raw,:erb,:html,:builder,:ruby,:slim,:coffee,:jbuilder]}。在:...
但是,应该只有条件1才会触发。
执行这种条件jQuery的最佳方法是什么?
我找到了一个解决方案,我删除了user/stream
中的斜杠:
if(window.location.href.indexOf('stream') >= 0){
alert("Condition 1 is met")
$('#invite_to_video').html("<%= j render partial: 'messages/contact_ir_video', :locals => {:message => Message.new} %>");
}
if(window.location.href.indexOf('stream') = 0) {
alert("Condition 2 is met")
$('#invite_to_video').html("<%= j render partial: 'messages/contact_ir_video', :locals => {:message => Message.new} %>");
}