[英]Passing javascript to Rails helper
我創建了一個簡單的 javascript,用於在我的application.js
文件下的瀏覽器上獲取 URL:
function get_url {
var url = window.location.href;
return url;
}
然后在我的helper/application_helper.rb
文件中,我調用了 javascript:
def get_full_url_helper
javascript_tag(
"get_url()\";"
)
end
我試圖將它傳遞給其他助手,看看它是否獲得了完整的 url:
def active_url
get_full_url_helper
byebug
end
在使用 byebug 而不是在瀏覽器上獲取完整 url 時,它會返回這個奇怪的<script>//<![CDATA[] get_url </script>
東西。 出於某種原因,它沒有調用正確的函數,所以我可以獲取文本 URL 等等。
知道我在這里缺少什么嗎?
你的function get_url
不應該是:
function get_url() { // <--- parentheses were missing in your sample
var url = window.location.href;
return url;
}
此外,調試問題可能是什么(並可能找出您的問題可能出在哪里)的一種簡單方法是將您的get_full_url_helper
修改為:
def get_full_url_helper
javascript_tag(
"console.log('hello!');"
)
end
然后在瀏覽器的開發者控制台選項卡中驗證它是否在您的頁面上被調用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.