ruby-on-rails require_tree参数必须是Rails 5升级应用程序中的目录

beq87vna  于 2023-06-25  发布在  Ruby
关注(0)|答案(5)|浏览(283)

我刚刚将我的应用程序从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'

我没有使用任何插件。这是一个相当简单/vanilla的应用程序。唯一的样式来自默认的scaffold.scss
是什么引起的?

bqf10yzr

bqf10yzr1#

我终于想通了。所以因为我正在进行升级,RailsDiff并没有告诉我我遗漏了什么。
所以错误消息并不错误,但是,我忘记做的是创建一个空目录。
在我的app/assets/javascripts/cable.js中,我有以下内容:

//= require_tree ./channels

但是我忘了创建这个文件夹。
所以要解决这个问题,我所要做的就是在app/assets/javascripts中创建一个空文件夹,名为channels。另外,由于git会忽略空目录,因此在新创建的文件夹中,我还必须创建一个名为.keep的空文件。
所以一旦我做了以下的事情,一切就像一个魅力:

  • 创建文件夹:app/assets/javascripts/channels
  • 在该文件夹中创建空文件:app/assets/javascripts/channels/.keep

现在一切都很完美。

yvgpqqbh

yvgpqqbh2#

面临着类似但不相同的问题。在Rails从5.2.3升级到www.example.com期间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

mxg2im7a

mxg2im7a3#

这个问题发生在使用rails new appname --skip-keeps标志时--它仍然试图要求不存在的文件,这通常是Rails团队的一个错误。
这只是一个不同的方法来描述的问题,主要的解决方案工作完美;
1.打开app/assets/javascripts/cable.js
1.删除第6行中自动生成的//= require_tree ./channels
保持你的代码库尽可能小,有人跳过了.keep的原因。

b5buobof

b5buobof4#

如果您没有使用ActionCable,请删除app/assets/javascripts/cable.js中的所有内容

iyr7buue

iyr7buue5#

这也发生在我身上,因为我把我的应用程序文件搞砸了。我首先在本地创建它,然后将文件上传到我的GitHub存储库,然后在删除本地文件后将其克隆到原始位置。这使得assets directory和vendor目录中的一些目录丢失。
我所做的是重新创建与manifest.js路径相关的它们。
因此,由于manifest.js路径是app/assets/config/manifest.js
例如:

//= link_tree ../images

在***资产***目录中创建***映像***目录

//= link_tree ../../../vendor/javascript .js

在***app***目录外创建***vendor***目录,并在***vendor***目录内创建***JavaScript***目录

相关问题