我的所有项目中的资产管道都存在问题,而且我正在试图找出原因
我有一个非常基本的测试javascript函数来测试它
HTML
<button onclick="myFunction()">Test</button>
的javascript
function myFunction() {
alert("I am an alert box!");
}
如果我把它放在视图中的脚本标签中,它可以正常工作。如果我把它放在application.js中它运行正常,但是一旦我尝试添加自己的文件它就不起作用,我试图在供应商中插入的任何插件都是如此。
首先我将它添加到清单
//= require rails-ujs
//= require turbolinks
//= require home.js
//= require_tree .
然后我将助手添加到我的视图
<%= javascript_include_tag "home", "data-turbolinks-track" => true %>
然后我预先编译它并重新启动服务器
Rails.application.config.assets.precompile += %w( home.js )
毕竟,它仍然无法奏效。我一直试图解决这个问题几天,到目前为止没有任何工作,我非常感谢我给予的任何帮助,谢谢你的帮助。
我从删除home的帮助程序并将application.js的帮助程序添加到我的视图中时出错:
rails-ujs.self-661556f443fbae7f6cec8f2cd394aa5e8186cb3f96aded9628126d3484eaa71a.js?body=1:626 Uncaught Error: rails-ujs has already been loaded!
at Object.Rails.start (rails-ujs.self-661556f443fbae7f6cec8f2cd394aa5e8186cb3f96aded9628126d3484eaa71a.js?body=1:626)
at rails-ujs.self-661556f443fbae7f6cec8f2cd394aa5e8186cb3f96aded9628126d3484eaa71a.js?body=1:675
at rails-ujs.self-661556f443fbae7f6cec8f2cd394aa5e8186cb3f96aded9628126d3484eaa71a.js?body=1:678
at rails-ujs.self-661556f443fbae7f6cec8f2cd394aa5e8186cb3f96aded9628126d3484eaa71a.js?body=1:679
at rails-ujs.self-661556f443fbae7f6cec8f2cd394aa5e8186cb3f96aded9628126d3484eaa71a.js?body=1:686
我的layout / application.html.erb文件
<html>
<head>
<title>PipelinePractice</title>
<%= csrf_meta_tags %>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
</head>
<body>
<%= yield %>
</body>
</html>
呈现给视图的内容:
<!DOCTYPE html>
<html>
<head>
<title>PipelinePractice</title>
<meta name="csrf-param" content="authenticity_token" />
<meta name="csrf-token" content="gcKJ/jjUrQd6pLwmYwjTn7N25/tTCUu43iKak+/uUgU06n0tBU7v7U58+cCMB/bXVI3K92d5Ii0A2dBCaJfTeg==" />
<link rel="stylesheet" media="all" href="/assets/home.self-e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855.css?body=1" data-turbolinks-track="reload" />
<link rel="stylesheet" media="all" href="/assets/application.self-f0d704deea029cf000697e2c0181ec173a1b474645466ed843eb5ee7bb215794.css?body=1" data-turbolinks-track="reload" />
<script src="/assets/rails-ujs.self-661556f443fbae7f6cec8f2cd394aa5e8186cb3f96aded9628126d3484eaa71a.js?body=1" data-turbolinks-track="reload"></script>
<script src="/assets/home.self-877aef30ae1b040ab8a3aba4e3e309a11d7f2612f44dde450b5c157aa5f95c05.js?body=1" data-turbolinks-track="reload"></script>
<script src="/assets/turbolinks.self-1d1fddf91adc38ac2045c51f0a3e05ca97d07d24d15a4dcbf705009106489e69.js?body=1" data-turbolinks-track="reload"></script>
<script src="/assets/action_cable.self-be3674a79bb9d13d41d259b2c17fad23aef20946dab3603b9d02374ea795005f.js?body=1" data-turbolinks-track="reload"></script>
<script src="/assets/cable.self-8484513823f404ed0c0f039f75243bfdede7af7919dda65f2e66391252443ce9.js?body=1" data-turbolinks-track="reload"></script>
<script src="/assets/application.self-66347cf0a4cb1f26f76868b4697a9eee457c8c3a6da80c6fdd76ff77e911715e.js?body=1" data-turbolinks-track="reload"></script>
</head>
<body>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="/assets/home.self-877aef30ae1b040ab8a3aba4e3e309a11d7f2612f44dde450b5c157aa5f95c05.js?body=1" data-turbolinks-track="true"></script>
</head>
<body>
<h2>JavaScript Alert</h2>
<button onclick="myFunction()">Try it</button>
<script>
</script>
</body>
</html>
</body>
</html>
答案 0 :(得分:1)
我刚遇到类似的问题。在我的情况下,文件夹app / assets / javascripts中还有一个名为home.coffee的文件,该文件阻止了home.js被rails看到。如果我将文件home.js重命名为test.js(不存在test.coffee)或删除了文件home.coffee,则一切正常。