2016-05-01 86 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 –