[英]How can I call upon a JS script in my /app/assets/javascripts folder from a View page in Rails?
I'm trying to add a live clock onto a webapp that I'm building with Rails and I've found an answer that gives me the JS for the clock. 我正在尝试将实时时钟添加到我使用Rails构建的Web应用程序中,并且找到了一个答案,该答案为我提供了时钟的JS。 The solution can be found here: How to show current time in JavaScript in the format HH:MM:SS? 解决方案可以在这里找到: 如何以HH:MM:SS格式显示JavaScript中的当前时间?
I've added the code to my /app/assets/javascripts folder under the title of 'time.js'. 我已将代码添加到我的/ app / assets / javascripts文件夹中,标题为“ time.js”。
How can I call the method in this script to run/render on my home page (called 'Home.html.erb' in the Views section of my webapp). 如何在此脚本中调用该方法以在主页上运行/渲染(在Webapp的“视图”部分中称为“ Home.html.erb”)。
Here is the code of the JS Clock Script: 这是JS Clock脚本的代码:
(function () {
function checkTime(i) {
return (i < 10) ? "0" + i : i;
}
function startTime() {
var today = new Date(),
h = checkTime(today.getHours()),
m = checkTime(today.getMinutes()),
s = checkTime(today.getSeconds());
document.getElementById('time').innerHTML = h + ":" + m + ":" + s;
t = setTimeout(function () {
startTime()
}, 500);
}
startTime();
})();
And here is the code in my 'home.html.erb' page where I'm trying to render the code: 这是我的“ home.html.erb”页面中的代码,我试图在其中呈现代码:
<div class="col box">
<p><i class="far fa-clock"></i><%= javascript_tag "startTime()" %></p>
</div>
I also have the following under config/initializers/assets.rb: 我在config / initializers / assets.rb下也有以下内容:
Rails.application.config.assets.precompile += %w( time.js )
And the following under 'application.html.erb': 在“ application.html.erb”下面:
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
<%= javascript_include_tag "time", 'data-turbolinks-track': 'reload' %>
Apologies for the lack of examples/unclear question earlier! 抱歉,之前没有示例/不清楚的问题!
TL;DR is that I'm trying to run the code in 'time.js' inside my 'home.html.erb' file but don't know how to do it. TL; DR是我正在尝试在“ home.html.erb”文件中的“ time.js”中运行代码,但不知道如何执行。
Regards, Llausa 问候,劳萨
You can use javascript_include_tag to tell the Asset Pipeline to include the Javascript file in the page 您可以使用javascript_include_tag通知素材资源管道在页面中包含Javascript文件
More on the Asset Pipeline and how to use javascript_include_tag here 有关资产管道的更多信息以及如何在此处使用javascript_include_tag
I'm unsure if this is correct as I am a litte confused but I understand that you are wanting to just run the JS script. 我不确定这是否正确,因为我有点困惑,但是我知道您只想运行JS脚本。 Assuming you've included the script in the home.html file then it should work. 假设您已经将该脚本包含在home.html文件中,那么它应该可以工作。
Check if you have correctly go to the right files ie using .../ for going backward in the directory. 检查您是否正确进入了正确的文件,即使用... /在目录中向后移动。
Failing this, and without any examples or link to your code, check that you have included the div with an id of time (as per example in link) as this is where it writes the HTML from the JS to. 如果失败,并且没有任何示例或代码链接,请检查是否已将div包含时间id(如链接中的示例所示),因为这是将JS中的HTML写入其中的位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.