为什么我的资产没有按照我希望的方式加载 - 开发模式。铁轨4.2

时间:2015-02-26 20:31:48

标签: ruby-on-rails ruby-on-rails-4 asset-pipeline

我不确定rails 4.2中的设置是否从4.1改变了,或者我是不是疯了。但这是我的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 vendor/assets/javascripts of plugins, if any, 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.
//
// Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require_tree .
//= require bootstrap
//= require react
//= require react_ujs
//= require backbone

//= require app
//= require app/helpers/core.js
//= require app/collections/aisis_writer_user
//= require app/routers/aisis_writer_panel
//= require app/views/aisis_writer_panel

它清楚地说明了我的资产应该如何加载。这些文件中有几个在顶部有自己的require语句。现在处于开发模式,从我看到的东西是我没有得到一个巨大的连接资产文件,而是每个资产作为脚本标签加载到页面的标题中。如果它遵循我已经规定的顺序,那就没问题了。

<script src="/assets/jquery.js?body=1" data-turbolinks-track="true"></script>
<script src="/assets/jquery_ujs.js?body=1" data-turbolinks-track="true"></script>
<script src="/assets/turbolinks.js?body=1" data-turbolinks-track="true"></script>
<script src="/assets/app/helpers/intervals.js?body=1" data-turbolinks-track="true"></script>
<script src="/assets/app/helpers/reset_intervals.js?body=1" data-turbolinks-track="true"></script>
<script src="/assets/app/routers/aisis_writer_panel.js?body=1" data-turbolinks-track="true"></script>
<script src="/assets/app.js?body=1" data-turbolinks-track="true"></script>
<script src="/assets/app/collections/aisis_writer_user.js?body=1" data-turbolinks-track="true"></script>
<script src="/assets/app/helpers/core.js?body=1" data-turbolinks-track="true"></script>
<script src="/assets/app/views/aisis_writer_panel.js?body=1" data-turbolinks-track="true"></script>
<script src="/assets/app/helpers/polling.js?body=1" data-turbolinks-track="true"></script>
<script src="/assets/bootstrap.js?body=1" data-turbolinks-track="true"></script>
<script src="/assets/react.js?body=1" data-turbolinks-track="true"></script>
<script src="/assets/react_ujs.js?body=1" data-turbolinks-track="true"></script>
<script src="/assets/backbone.js?body=1" data-turbolinks-track="true"></script>
<script src="/assets/application.js?body=1" data-turbolinks-track="true"></script>

这不是我如何指定要订购的东西,因此我得到了:

uncaught ReferenceError: App is not defined
aisis_writer_user.js:1 Uncaught ReferenceError: Backbone is not defined
aisis_writer_panel.js:1 Uncaught ReferenceError: Backbone is not defined
backbone.js:219 Uncaught TypeError: Cannot read property 'each' of undefined

我订购东西的方式,这些错误不应该 - 或者至少不是全部 - 都会出现。

所以我的问题是:当我告诉它以特定的方式加载文件时,为什么资产管道会忽略我?

1 个答案:

答案 0 :(得分:1)

  

当我告诉资产管道以特定方式加载文件时,为什么资产管道会忽略我?

不是,它正是你所要求的。 忽略了require_tree .语句中间的大require

require_tree .的重点是递归地包含它在你给它的目录中找到的所有文件。你给它.,这样你就可以将.js目录中的每个.coffeeapp/assets/javascripts文件转储到你的清单中,无论如何他们碰巧从磁盘上读取它们。

如果您不想要这种行为,则需要删除该行。