简体   繁体   English

无法在Rails(5+)中加载控制器特定的javascript文件

[英]Unable to load controller-specific javascript file in Rails (5+)

I have tried as many potential solutions as I can find and, so far, nothing has worked. 我已经尝试了尽可能多的潜在解决方案,到目前为止,没有任何效果。 The code in the .js file is not being executed nor does it appear to be present anywhere in the client browser. .js文件中的代码未执行,也未显示在客户端浏览器中的任何位置。

application.html.erb loads this partial in the head: application.html.erb将此部分加载到头部:

<%= csrf_meta_tags %>
<%= stylesheet_link_tag "application", params[:controller], media: "all" %>
<%= javascript_include_tag "application", params[:controller] %>

config/initializers/assets.rb 配置/初始化/ assets.rb

%w(  articles password_resets sessions static_pages users ).each do |controller|
  Rails.application.config.assets.precompile += ["#{controller}.js.coffee", "#{controller}.css"]
end

[controller-name-here].js [控制器名称这里]的.js

$(document).ready(function() {
    alert('My controller');
});

In the browser, when everything above is as it is, there will be an asset called '[controller-name].self.longstringhere.js?body=1'. 在浏览器中,当以上所有操作保持不变时,将存在一个名为“ [controller-name] .self.longstringhere.js?body = 1”的资产。 It will have this code in it: 它将包含以下代码:

(function() {


}).call(this);

… which does not exist anywhere in my project. …在我的项目中不存在。 If I remove the controller from the assets.rb, when trying to load a page from that controller, it throws an error [as expected]. 如果我从assets.rb中删除该控制器,则当尝试从该控制器加载页面时,它会引发错误(按预期)。 When getting rid of the entry in the application layout, the '[controller].self.js' file is missing from the browser's assets [as expected]. 当摆脱应用程序布局中的条目时,浏览器的资产中缺少[[controller] .self.js]文件[正如预期的那样]。 However, whether or not any of the controllers' js files are missing or not doesn't cause any errors and does not preclude the browser's assets from including the '[controller].self.js'. 但是,是否缺少控制器的js文件不会导致任何错误,也不会阻止浏览器的资产包含'[controller] .self.js'。 It is like the files in app/assets/javascripts are totally ignored and “stubbed out”. 就像app / assets / javascripts中的文件被完全忽略并“存根”。

NOTE: I specifically do not have '//= require_tree .' 注意:我专门没有'// = require_tree'。 and '*= require_tree .' 和'* = require_tree'。 in the application.js/.css files as I am specifically following the instructions here . 在application.js / .css文件中,因为我特别按照此处的说明进行操作。 This is intentional. 这是故意的。

I have also removed all references that I can find (gem's, assets, etc.) of turbolinks as I have been reading that this can cause evil things to happen that may relate to this issue. 我还删除了所有可以找到的turbolinks参考(宝石,资产等),因为我一直在阅读,这可能导致发生与该问题有关的邪恶事件。

I have also restarted the server, precompiled the assets, and have done this and several other ritualistic things many times - all to no avail. 我还重新启动了服务器,对资产进行了预编译,并多次执行了此操作以及其他一些仪式性的操作-均无济于事。

For the record, I also changed the "#{controller}.js.coffee" in the assets.rb to just '.js' to no avail. 作为记录,我也将assets.rb中的“#{controller} .js.coffee”更改为仅.js,但无济于事。 But I read that this isn't necessary anyway. 但是我读到,这根本没有必要。

This problem is with all controller's – not just one in particular. 这个问题与所有控制器有关,而不仅仅是一个控制器。

Thanks for your help! 谢谢你的帮助!

[EDIT] I created a new rails project using scaffolding and followed the same procedure (but ensuring that the precompiled asset (posts.js) was called out specifically by name in assets.rb) and have the same problem as described above including the same javascript "stub" code in the browser's posts.blahblah.js file. [编辑]我使用脚手架创建了一个新的Rails项目,并遵循相同的步骤(但要确保预先编译的资产(posts.js)在asset.rb中是按名称专门调用的),并且存在与上述相同的问题,包括相同的问题。浏览器的posts.blahblah.js文件中的javascript“ stub”代码。

Seems like your file name is [controller-name-here].js and you are using js not coffee. 似乎您的文件名为[controller-name-here].js而您使用的是js,而不是coffee。

But you are adding coffee file in your config/initializers/assets.rb file. 但是,您要在config/initializers/assets.rb文件中添加coffee文件。 that's why it's not able to compile file properly. 这就是为什么它不能正确编译文件的原因。 Try change your config/initializers/assets.rb file like below: 尝试更改您的config/initializers/assets.rb文件,如下所示:

%w(  articles password_resets sessions static_pages users ).each do |controller|
  Rails.application.config.assets.precompile += ["#{controller}.js", "#{controller}.css"]
end

Everything else seems fine. 其他一切似乎都很好。

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

相关问题 Rails 4 / Sprockets:特定于控制器的JavaScript随处可用? - Rails 4 / Sprockets: controller-specific JavaScripts are available everywhere? 在symfony2中管理控制器特定的javascript有哪些选项? - What are the options for managing controller-specific javascript in symfony2? 如何确保在控制器特定的JS之后调用Rails资产管道中的JQuery脚本? - How do I make sure JQuery scripts in Rails asset pipeline are called after controller-specific JS? 如何使用javascript_include_tag从供应商/资产/ javascripts添加控制器特定的javascript? - How to add a controller-specific javascript from vendor/assets/javascripts using javascript_include_tag? 根据Rails中的控制器加载JavaScript文件 - Load JavaScript file according to controller in Rails Rails应用程序未加载特定的JavaScript文件 - Rails app does not load a specific JavaScript file 轨道5:每个控制器加载Javascript - RAILS 5 : Load Javascript per Controller Angular-从另一个控制器调用特定于控制器的函数的最佳实践是什么? - Angular— What is the best practice for calling a controller-specific function from another controller? 如何在文档头中使用AngularJS控制器特定的作用域值? - How can I use AngularJS controller-specific scope values in document head? Rails中特定于文件的JavaScript - File-specific JavaScript in Rails
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM