简体   繁体   English

如何从Rails的“查看”页面中的/ app / assets / javascripts文件夹中调用JS脚本?

[英]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.

相关问题 Rails 4:application.js中的“ // = require_tree”在/ app / assets / javascripts中不包含我的js文件 - Rails 4: “//= require_tree .” in application.js doesn't include my js file in /app/assets/javascripts Rails递归地包含javascripts资源文件夹 - Rails include javascripts assets folder recursively rails - 如何升级我的jquery,我没有在app / assets中看到它,但它在application.js中被引用? - rails - how can I upgrade my jquery, I don't see it in app/assets but it's referenced in application.js? AMP-HTML页面如何从rails / app / assets / javascript文件夹加载javascript? - How does AMP-HTML page load javascript from rails/app/assets/javascript folder? Heroku和Rails 4:Heroku在资产/ JavaScripts下找不到我的嵌套文件夹 - Heroku and Rails 4: Heroku isn't finding my nested folder under assets/javascripts `application.js`不再包括Rails 3.1应用程序的app / assets / javascripts中的所有脚本 - `application.js` no longer including all scripts in app/assets/javascripts for Rails 3.1 app 我如何拥有一个可以在我的 React 应用程序中的任何文件中使用的资产文件,而不必总是导航到资产文件夹 - How do i have an assets file that i can use in any file in my react app without always having to navigate to the assets folder 如何从rails应用程序中的assets / images文件夹中获取图像 - how to fetch image from the assets/images folder in a rails app 如何调用动作,从js传递值到该动作并在Rails 3的页面上重新呈现js? - How can I call an action, pass a value to the action from js and re-render the js on a page in rails 3? 将JS代码迁移到Rails应用程序中的资产/ JavaScript之后,会话变量不再被识别 - session variable is not recognized anymore after migrating js code to assets/javascripts in rails app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM