我很好奇Rails 5如何渲染局部图像,以及是否存在我不知道的陷阱。我的页面上有两个部分,一个部分呈现为html的一部分,另一个部分呈现在ajax请求的成功回调中。但是,当我刷新页面时,即使没有触发ajax请求,实际上也会呈现ajax请求内部的部分,并且甚至在注释掉呈现部分的调用时也被呈现。
我一直假设部分代码在运行代码行时呈现。
这里是相关代码:
$('body').on("click", ".notif-popup-container", function() {
$.post(
"/notifications/read",
{ notif_id: notifId },
).done(function(data) {
$('#notifUnreadCount').html(data.unread_notifs);
var popover = $('#notificationsBell').data('bs.popover');
popover.config.content = $('.notifications-list').html();
})
$('.notifications-list').html("<%= j (render partial: 'notifications/partials/test_partial') %>"); // this never fails to render, even when commented out
})
<div style="display:none" class="notifications-list">
<%= render 'notifications/partials/popover_notifications' %>
</div>
这里是控制台输出图像。如您所见,_popover_notifications.html.erb会按照其应有的方式进行渲染,但是我的_test_partial.html.erb也应进行渲染,这是不应该的,因为它所在的单击处理程序没有被触发。
任何见识表示赞赏。
问题在第4行上。===
operator in Ruby甚至与其他语言(如JavaScript或PHP)都不接近。
您实际上应该首先使用===
,并避免与0,null或true / false进行比较。 Ruby是一种完全面向对象的语言。
.any?
也执行数据库查询并在视图中分配变量是一种反模式。您的视图应仅从控制器获取数据并以最简单的方式呈现它。用数据库查询乱写您的视图将导致性能问题。