2016-05-01 81 views
0

我试图了解js如何在Rails中工作。Rails - Bootstrap popover js

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

我目前在我的组织视图文件夹中有一个名为_preferences的部分。在我的组织展示页面中,我渲染了这个部分。该部分有一个按钮,调用酥料饼,如:

<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> 

我试图让一个organisations.js文件有:

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

,但是当我尝试这种方法的popovers没有工作。

当我将以下内容添加到偏好偏好的底部时,它一切正常。

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

我预计第一次尝试工作。我不明白为什么它没有。有人看到错误吗?

回答

0

您没有使用turbolinks这就是你已经创建正处于这是一个不好的做法,如果你想使用turbolinks正常那么你的应用程序会更快的文件<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %> 的头部取出way.The文件。在添加的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

+0

我turbolinks =>假的(因为我用olark) – Mel

+0

如果你有turbolinks关闭,再次证明您所创建的文件被取在文档的头部和turbolinks没有被跟踪,所以脚本在页面加载时不能正常运行。 –

+0

我明白你使用的是olark,但这并不意味着你不应该使用turbolinks还有comaptability脚本。 https://www.olark.com/help/turbolinks –