简体   繁体   English

将 javascript 传递给 Rails 助手

[英]Passing javascript to Rails helper

I created a simple javascript on getting the URL on my browser under my application.js file:我创建了一个简单的 javascript,用于在我的application.js文件下的浏览器上获取 URL:

function get_url {
 var url = window.location.href;
 return url;
}

Then on my helper/application_helper.rb file I called the javascript:然后在我的helper/application_helper.rb文件中,我调用了 javascript:

def get_full_url_helper
 javascript_tag(
     "get_url()\";"
 )
end

I tried to pass this on other helper just to see if its getting the full url:我试图将它传递给其他助手,看看它是否获得了完整的 url:

def active_url
  get_full_url_helper
  byebug
end

Upon using byebug instead of getting the full url on the browser it returns this weird <script>//<![CDATA[] get_url </script> thing.在使用 byebug 而不是在浏览器上获取完整 url 时,它会返回这个奇怪的<script>//<![CDATA[] get_url </script>东西。 For some reason its not calling the right function so I can get the text URL and so something with it.出于某种原因,它没有调用正确的函数,所以我可以获取文本 URL 等等。

Any idea what am I missing here?知道我在这里缺少什么吗?

Shouldn't your function get_url be:你的function get_url不应该是:

function get_url() { // <--- parentheses were missing in your sample
 var url = window.location.href;
 return url;
}

Also, a simple way to debug what the issue might be (and perhaps find out where your issue might be) is to just modify your get_full_url_helper to be:此外,调试问题可能是什么(并可能找出您的问题可能出在哪里)的一种简单方法是将您的get_full_url_helper修改为:

def get_full_url_helper
 javascript_tag(
     "console.log('hello!');"
 )
end

and then verify whether it is being invoked on your page in developer console tab in your browser.然后在浏览器的开发者控制台选项卡中验证它是否在您的页面上被调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM