Dev 中 Rails 6 中资产的错误 404(未找到)
Error 404 (Not Found) for assets in Rails 6 in Dev
我已经尝试了一个多星期,试图将自定义模板放入版本 6 的 Rails 应用程序中。无论是使用 Yarn 还是直接在 html 中引用,都找不到资产。
GET http://localhost:3000/assets/vendors/iconfonts/mdi/css/materialdesignicons net::ERR_ABORTED 404 (Not Found)
资产位于 Vendor 文件夹中。请注意,文件夹内还有另一个名为 "vendors" 的文件夹,因此这不是一个错误。
我有 application.js
require("@rails/ujs").start()
require("turbolinks").start()
require("@rails/activestorage").start()
require("channels")
require("jquery")
require("popper.js")
require("bootstrap")
require('vendors/js/vendor.bundle.base')
require('vendors/js/vendor.bundle.addons')
require('js/shared/off-canvas')
require('js/shared/misc')
我也尝试过使用另一个遵循资产管道最佳实践的模板,但它现在不像这个那样工作。
This is the path wehre the template is.
我的 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, 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
*/
@import "vendors/iconfonts/mdi/css/materialdesignicons";
@import "vendors/iconfonts/ionicons/css/ionicons";
@import "vendors/iconfonts/typicons/src/font/typicons";
@import "vendors/iconfonts/flag-icon-css/css/flag-icon";
@import "vendors/css/vendor.bundle.base";
@import "vendors/css/vendor.bundle.addons";
@import "css/shared/style";
@import "css/demo_1/style";
不过我也试过其他的方法,用require,之前说过直接在[=53=里面链接],现在是这样的:
<!DOCTYPE html>
<html>
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>School</title>
<%= csrf_meta_tags %>
<%= csp_meta_tag %>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_packs_with_chunks_tag 'application' %>
<link rel="shortcut icon" href="/images/favicon.png" />
</head>
<body>
...
我已经 运行 命令:
rails webpacker:install
rails assets:precompile
另外,我更改了 application.rb 添加
config.serve_static_assets = true
,但似乎没有任何效果,我的页面总是没有主题。
Rails版本:6.0.2.1
Ruby 版本:2.6.5p114(2019-10-01 修订版 67812)[x64-mingw32]
节点版本:v12.14.1
纱线版本:1.21.1
所以:Windows10
我不知道该怎么做才能修复 'Not Found' 错误。
您的 application.js 需要移动到不同的文件夹以便 webpacker 能够使用它。
app/javascript/packs/application.js
我已经尝试了一个多星期,试图将自定义模板放入版本 6 的 Rails 应用程序中。无论是使用 Yarn 还是直接在 html 中引用,都找不到资产。
GET http://localhost:3000/assets/vendors/iconfonts/mdi/css/materialdesignicons net::ERR_ABORTED 404 (Not Found)
资产位于 Vendor 文件夹中。请注意,文件夹内还有另一个名为 "vendors" 的文件夹,因此这不是一个错误。
我有 application.js
require("@rails/ujs").start()
require("turbolinks").start()
require("@rails/activestorage").start()
require("channels")
require("jquery")
require("popper.js")
require("bootstrap")
require('vendors/js/vendor.bundle.base')
require('vendors/js/vendor.bundle.addons')
require('js/shared/off-canvas')
require('js/shared/misc')
我也尝试过使用另一个遵循资产管道最佳实践的模板,但它现在不像这个那样工作。
This is the path wehre the template is.
我的 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, 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
*/
@import "vendors/iconfonts/mdi/css/materialdesignicons";
@import "vendors/iconfonts/ionicons/css/ionicons";
@import "vendors/iconfonts/typicons/src/font/typicons";
@import "vendors/iconfonts/flag-icon-css/css/flag-icon";
@import "vendors/css/vendor.bundle.base";
@import "vendors/css/vendor.bundle.addons";
@import "css/shared/style";
@import "css/demo_1/style";
不过我也试过其他的方法,用require,之前说过直接在[=53=里面链接],现在是这样的:
<!DOCTYPE html>
<html>
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>School</title>
<%= csrf_meta_tags %>
<%= csp_meta_tag %>
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_packs_with_chunks_tag 'application' %>
<link rel="shortcut icon" href="/images/favicon.png" />
</head>
<body>
...
我已经 运行 命令:
rails webpacker:install
rails assets:precompile
另外,我更改了 application.rb 添加
config.serve_static_assets = true
,但似乎没有任何效果,我的页面总是没有主题。
Rails版本:6.0.2.1
Ruby 版本:2.6.5p114(2019-10-01 修订版 67812)[x64-mingw32]
节点版本:v12.14.1
纱线版本:1.21.1
所以:Windows10
我不知道该怎么做才能修复 'Not Found' 错误。
您的 application.js 需要移动到不同的文件夹以便 webpacker 能够使用它。
app/javascript/packs/application.js