【发布时间】:2020-07-25 17:11:00
【问题描述】:
我很好奇 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 也会呈现,但它不应该呈现,因为它所在的点击处理程序没有被触发。
任何见解都值得赞赏。
【问题讨论】:
-
能否请您在整个项目中使用 grep 'test_partial',也许您可以在其他地方渲染它?也许在'popover_notifications.html.erb'里面?
-
嗨@Vasilisa 不幸的是,这是我唯一拥有它的地方
标签: javascript jquery ruby-on-rails ruby-on-rails-5 partials