2016-12-29 31 views
2

我一直on Rails的教程以下邁克爾·哈特爾的Ruby和我無法加載JavaScript依賴關係第8章的一部分:https://www.railstutorial.org/book/basic_loginRails的資產管道不加載JavaScript的依賴性

的CSS加載依賴罰款(我使用引導程序),並且在瀏覽器中檢查頁面時,頭部有多個<link rel="stylesheet...>標籤。但是,只有一個腳本標記會加載已編譯的application.js文件。

有趣的是,即使我在js manifest文件中犯了錯誤,例如需要一個不存在的文件,我在瀏覽器中渲染我的頁面時也不會出現任何錯誤。

我試過在新鮮的應用程序中重現這一點,但我無法。只需要Gemfile中需要的寶石,即bundle install以及訪問除默認導航欄之外的任何頁面,將包括application.js清單文件中列出的所有依賴項,作爲單獨的<script src=..>標籤,位於<head>中。

我加載的資產爲layouts/application.html.erb文件的一部分,使用助手:

<%= csrf_meta_tags %> 
<%= stylesheet_link_tag 'application', media: 'all', 
            'data-turbolinks-track': 'reload' %> 
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %> 

application.js清單文件:

// This is a manifest file that'll be compiled into application.js, which will include all the files 
// listed below. 
// 
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts, 
// or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path. 
// 
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the 
// compiled file. JavaScript code in this file should be added after the last require_* statement. 
// 
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details 
// about supported directives. 
// 
//= require jquery 
//= require jquery_ujs 
//= require bootstrap 
//= require turbolinks 
//= require_tree . 

我的回報率和Ruby版本是5.1和2.3.2分別。
JS的清單文件是app/assets/javascripts/application.js

+0

是的引導寶石在那裏,但是,它根本不會加載任何依賴。 – Vlad

回答

1

我通過我的主要應用程序的相關部分移動到一個新的Rails應用程序是沒有加載JS的依賴關係解決問題。我期望這是我的一個配置問題。

更具體地說,我將Gemfile,db /,config/routes.rb和app /(config /除外)中的所有內容都移動到了新的rails應用程序中。一旦一切正常,我用新鮮的應用程序覆蓋了我的主應用程序的應用程序/目錄。