[英]Correct way to use coffee script in rails5
我正在開發我的第一個Rails應用程序,並嘗試通過單擊頁面中的另一個鏈接來顯示/隱藏視圖中的特定Web元素。 以下是我的看法,
%p
= link_to 'Show additional details', "#", id: "secondary-link"
%table{:id => "secondary"}
%tr
%th Key
%th Value
其中“輔助鏈接”是顯示/隱藏我的表格的鏈接。 第一次加載頁面時,該表將由我的application.css.scss中的以下行隱藏
#secondary{
display: none;
}
另外,我在我的assets / javascripts / application.coffee中添加了以下腳本
@myFunction = (variable) ->
$(document).on "page:change", ->
$('#secondary-link').click ->
$('#secondary').toggle()
但是,當我單擊頁面中的鏈接時,未顯示該表。 但是,在控制台中,如果我輸入“ myFunction”,我將獲得該功能
ƒ (variable) {
return $(document).on("page:change", function() {
return $('#secondary-link').click(function() {
return $('#secondary').toggle();
});
});
}
當我輸入myFunction()時出現錯誤
Uncaught TypeError: Cannot read property 'on' of undefined
at myFunction (application.self-4e74630fd8894bca1e22a8d66c1d7ebfaa39edf4f40e058aae03fc788a9f6d94.js?body=1:3)
at <anonymous>:1:1
感謝您的幫助。
我不確定為什么會出現此問題,但我直接在application.js文件中使用jquery修復了該問題,並刪除了coffee文件。 我的js文件具有以下代碼來處理事件。
$(document).ready(function() {
$('#secondary-link').click(function(event){
return $('#secondary').toggle();
});
});
我已經停止並重新啟動服務器,並且此方法有效。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.