require_tree 参数必须是 Rails 5 升级应用程序中的目录
Posted
技术标签:
【中文标题】require_tree 参数必须是 Rails 5 升级应用程序中的目录【英文标题】:require_tree argument must be a directory in a Rails 5 upgraded app 【发布时间】:2017-01-10 17:09:49 【问题描述】:我刚刚将我的应用程序从 Rails 4.2.7
升级到 Rails 5.0.0.1
。我使用RailsDiff 来确保我已经涵盖了所有内容,并且我相信我做到了。到目前为止,在加载我的应用程序之前,一切都运行良好。
现在我看到了这个错误:
Sprockets::ArgumentError at /
require_tree argument must be a directory
这是我的application.css
:
/*
* This is a manifest file that'll be compiled into application.css, which will include all the files
* listed below.
*
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
* or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
*
* You're free to add application-wide styles to this file and they'll appear at the bottom of the
* compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
* files in this directory. Styles in this file should be added after the last require_* statement.
* It is generally better to create a new file per style scope. *
*= require_tree .
*= require_self
*/
这是我的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 turbolinks
//= require_tree .
这是服务器日志的样子:
Started GET "/" for ::1 at 2016-09-02 09:08:19 -0500
ActiveRecord::SchemaMigration Load (1.5ms) SELECT "schema_migrations".* FROM "schema_migrations"
User Load (1.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2 [["id", 2], ["LIMIT", 1]]
Processing by ProfilesController#index as html
Rendering profiles/index.html.erb within layouts/application
Profile Load (1.6ms) SELECT "profiles".* FROM "profiles"
Rendered profiles/index.html.erb within layouts/application (45.8ms)
Completed 500 Internal Server Error in 367ms (ActiveRecord: 6.3ms)
DEPRECATION WARNING: #original_exception is deprecated. Use #cause instead. (called from initialize at /.rvm/gems/ruby-2.3.1@myapp/gems/better_errors-2.1.1/lib/better_errors/raised_exception.rb:7)
DEPRECATION WARNING: #original_exception is deprecated. Use #cause instead. (called from initialize at /.rvm/gems/ruby-2.3.1myapp/gems/better_errors-2.1.1/lib/better_errors/raised_exception.rb:8)
Sprockets::ArgumentError - require_tree argument must be a directory:
sprockets (3.7.0) lib/sprockets/directive_processor.rb:182:in `rescue in block in process_directives'
sprockets (3.7.0) lib/sprockets/directive_processor.rb:179:in `block in process_directives'
sprockets (3.7.0) lib/sprockets/directive_processor.rb:178:in `process_directives'
我没有使用任何类型的插件。这是一个相当简单/香草的应用程序。唯一的样式来自默认的scaffold.scss
。
这可能是什么原因造成的?
【问题讨论】:
【参考方案1】:我终于明白了。所以因为我正在进行升级,RailsDiff 并没有告诉我我遗漏了什么。
所以错误信息并没有错,但是,我忘记做的是创建一个空目录。
在我的app/assets/javascripts/cable.js
中,我有以下内容:
//= require_tree ./channels
但是,我忘了实际创建该文件夹。
所以要解决这个问题,我所要做的就是在app/assets/javascripts
中创建一个名为channels
的空文件夹。此外,由于 git 忽略了空目录,因此在新创建的文件夹中,我还必须创建一个名为 .keep
的空文件。
所以一旦我做了以下事情,一切都像魅力一样:
创建文件夹:app/assets/javascripts/channels
在该文件夹中创建空文件:app/assets/javascripts/channels/.keep
现在一切正常。
【讨论】:
2018 年仍在工作 - 感谢您发布此内容 - 为我工作! 就我而言,我从 javascript sprockets 转换为 webpacker,但忘记删除引用 app/assets/config/manifest.js 中旧 javascript 位置的行 2021 年仍在工作,我正在从 Rails4.2.11.1
升级到 Rails 5.0.1
。感谢您的澄清。正如你所说,控制台中的错误是错误的。
谢谢,节省我的时间【参考方案2】:
使用rails new appname --skip-keeps
标志时会出现问题 - 它仍然尝试要求不存在的文件,通常是 Rails 团队的错误。
这只是描述问题的不同方法,主要解决方案完美运行;
-
打开
app/assets/javascripts/cable.js
从第 6 行删除自动生成的 //= require_tree ./channels
让你的代码库尽可能小,有人跳过.keep
s 是有原因的。
【讨论】:
我同意代码库尽可能小。我在升级 rails 时遇到了问题。【参考方案3】:面临类似但不相同的问题。在将 Rails 从 5.2.3 升级到 5.2.4.1 期间
$ rails s
返回:
Expected to find a manifest file in `app/assets/config/manifest.js` (Sprockets::Railtie::ManifestNeededError)
But did not, please create this file and use it to link any assets that need to be rendered by your app:
Example:
//= link_tree ../images
//= link_directory ../javascripts .js
//= link_directory ../stylesheets .css
and restart your server
好的,按照说明,用上面的内容创建manifest.js
,然后
$ rails s
返回:
Sprockets::ArgumentError at / link_tree argument must be a directory
修复:
在新的images
文件夹中创建空的.keep
文件(可能在过去的某个时间点被删除而没有任何直接后果):
app/assets/images/.keep
【讨论】:
【参考方案4】:如果您不使用 ActionCable
然后删除了app/assets/javascripts/cable.js
中的所有内容
【讨论】:
以上是关于require_tree 参数必须是 Rails 5 升级应用程序中的目录的主要内容,如果未能解决你的问题,请参考以下文章
Rails 3.1 资产管道和手动订购的 Javascript 需要