Rails - Bootstrap popover js

Rails - Bootstrap popover js

本文关键字:js popover Bootstrap Rails      更新时间:2023-09-26

我试图了解js在Rails中是如何工作的。

我正在使用bootstrap,并设法使popovers工作,但不是以我理解Rails工作的方式。

我目前在我的组织视图文件夹中有一个名为_preferences的部分。在我的组织展示页面中,我将其部分化。分部有一个按钮,可以调用popover,如:

 <button class="fa fa-info-circle fa-2x fa-border btn-info" id="publicity" href="#"  rel="popover" data-original-title="Publicity Issues" data-content="<%= publicity_popover(@organisation.preference)%>"></button>

我试图制作一个organizations.js文件,其中包含:

$('#publicity').popover()
$('#academic_publication').popover()
$('#presentations').popover()

但当我尝试这种方法时,popovers并没有起作用。

当我将以下内容添加到首选项部分的底部时,一切都很好。

<script>
$('#publicity').popover()
$('#academic_publication').popover()
$('#presentations').popover()
</script>

我期待着第一次尝试工作。我不明白为什么没有。有人看到错误了吗?

您没有使用涡轮链接。您创建的文件是在文档<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %> 的开头提取的,这是一种糟糕的做法,如果您正确使用涡轮链接,那么您的应用程序会更快。在application.js中添加:

ready = function(){
$('#publicity').popover();
$('#academic_publication').popover();
$('#presentations').popover();
};
$(document).ready(ready);
$(document).on('page:load', ready);

在上阅读更多信息https://github.com/turbolinks/turbolinks-classic