带有 Webpack 和 Yarn 的 Rails 6 上的 ActionController 路由错误
Posted
技术标签:
【中文标题】带有 Webpack 和 Yarn 的 Rails 6 上的 ActionController 路由错误【英文标题】:ActionController Routing Error on Rails 6 w/ Webpack and Yarn 【发布时间】:2022-01-19 17:40:10 【问题描述】:第一次使用 react 和 webpack 配置生产 rails 应用程序。我试图了解由 rails 控制的 webpack 静态服务我的资产所需的结构。目前在我的 webpacker.yml 配置中遇到了一个愚蠢的错误。非常感谢任何帮助!
设置 pod running rails s -e production 后 log/production.log 出错:
I, [2021-09-12T15:44:38.761451 #99155] INFO -- : [3a2db351-9e29-49a1-86fd-c86aae7d7170] Started GET "/" for 127.0.0.1 at 2021-09-12 15:44:38 -0700
I, [2021-09-12T15:44:38.773079 #99155] INFO -- : [3a2db351-9e29-49a1-86fd-c86aae7d7170] Processing by HomeController#index as html
I, [2021-09-12T15:44:38.782973 #99155] INFO -- : [3a2db351-9e29-49a1-86fd-c86aae7d7170] Rendering home/index.html.erb within layouts/application
I, [2021-09-12T15:44:38.784041 #99155] INFO -- : [3a2db351-9e29-49a1-86fd-c86aae7d7170] Rendered home/index.html.erb within layouts/application (Duration: 1.0ms | Allocations: 529)
I, [2021-09-12T15:44:38.784881 #99155] INFO -- : [3a2db351-9e29-49a1-86fd-c86aae7d7170] Completed 200 OK in 12ms (Views: 4.2ms | Allocations: 5206)
I, [2021-09-12T15:44:38.905772 #99155] INFO -- : [96ccf555-b82c-443e-9244-807aab47e7f8] Started GET "/assets/application-5abc9724d464865c6ee8976394f0a5c56c7146787659b1e3a4a7ab5e5aaa4286.css" for 127.0.0.1 at 2021-09-12 15:44:38 -0700
I, [2021-09-12T15:44:38.908708 #99155] INFO -- : [7c4c2892-b369-424f-8372-a0976d15bbf1] Started GET "/packs/js/application-fc1aefbe06067b06814f.js" for 127.0.0.1 at 2021-09-12 15:44:38 -0700
F, [2021-09-12T15:44:39.064063 #99155] FATAL -- : [96ccf555-b82c-443e-9244-807aab47e7f8]
[96ccf555-b82c-443e-9244-807aab47e7f8] ActionController::RoutingError (No route matches [GET] "/assets/application-5abc9724d464865c6ee8976394f0a5c56c7146787659b1e3a4a7ab5e5aaa4286.css"):
[96ccf555-b82c-443e-9244-807aab47e7f8]
[96ccf555-b82c-443e-9244-807aab47e7f8] actionpack (6.0.4) lib/action_dispatch/middleware/debug_exceptions.rb:36:in `call'
[96ccf555-b82c-443e-9244-807aab47e7f8] actionpack (6.0.4) lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'
[96ccf555-b82c-443e-9244-807aab47e7f8] railties (6.0.4) lib/rails/rack/logger.rb:37:in `call_app'
[96ccf555-b82c-443e-9244-807aab47e7f8] railties (6.0.4) lib/rails/rack/logger.rb:26:in `block in call'
[96ccf555-b82c-443e-9244-807aab47e7f8] activesupport (6.0.4) lib/active_support/tagged_logging.rb:80:in `block in tagged'
[96ccf555-b82c-443e-9244-807aab47e7f8] activesupport (6.0.4) lib/active_support/tagged_logging.rb:28:in `tagged'
[96ccf555-b82c-443e-9244-807aab47e7f8] activesupport (6.0.4) lib/active_support/tagged_logging.rb:80:in `tagged'
[96ccf555-b82c-443e-9244-807aab47e7f8] railties (6.0.4) lib/rails/rack/logger.rb:26:in `call'
[96ccf555-b82c-443e-9244-807aab47e7f8] actionpack (6.0.4) lib/action_dispatch/middleware/remote_ip.rb:81:in `call'
[96ccf555-b82c-443e-9244-807aab47e7f8] actionpack (6.0.4) lib/action_dispatch/middleware/request_id.rb:27:in `call'
[96ccf555-b82c-443e-9244-807aab47e7f8] rack (2.2.3) lib/rack/method_override.rb:24:in `call'
[96ccf555-b82c-443e-9244-807aab47e7f8] rack (2.2.3) lib/rack/runtime.rb:22:in `call'
[96ccf555-b82c-443e-9244-807aab47e7f8] activesupport (6.0.4) lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
[96ccf555-b82c-443e-9244-807aab47e7f8] actionpack (6.0.4) lib/action_dispatch/middleware/executor.rb:14:in `call'
[96ccf555-b82c-443e-9244-807aab47e7f8] rack (2.2.3) lib/rack/sendfile.rb:110:in `call'
[96ccf555-b82c-443e-9244-807aab47e7f8] actionpack (6.0.4) lib/action_dispatch/middleware/host_authorization.rb:82:in `call'
[96ccf555-b82c-443e-9244-807aab47e7f8] railties (6.0.4) lib/rails/engine.rb:527:in `call'
[96ccf555-b82c-443e-9244-807aab47e7f8] puma (3.12.6) lib/puma/configuration.rb:227:in `call'
[96ccf555-b82c-443e-9244-807aab47e7f8] puma (3.12.6) lib/puma/server.rb:706:in `handle_request'
[96ccf555-b82c-443e-9244-807aab47e7f8] puma (3.12.6) lib/puma/server.rb:476:in `process_client'
[96ccf555-b82c-443e-9244-807aab47e7f8] puma (3.12.6) lib/puma/server.rb:334:in `block in run'
[96ccf555-b82c-443e-9244-807aab47e7f8] puma (3.12.6) lib/puma/thread_pool.rb:135:in `block in spawn_thread'
F, [2021-09-12T15:44:39.065224 #99155] FATAL -- : [7c4c2892-b369-424f-8372-a0976d15bbf1]
[7c4c2892-b369-424f-8372-a0976d15bbf1] ActionController::RoutingError (No route matches [GET] "/packs/js/application-fc1aefbe06067b06814f.js"):
[7c4c2892-b369-424f-8372-a0976d15bbf1]
[7c4c2892-b369-424f-8372-a0976d15bbf1] actionpack (6.0.4) lib/action_dispatch/middleware/debug_exceptions.rb:36:in `call'
[7c4c2892-b369-424f-8372-a0976d15bbf1] actionpack (6.0.4) lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'
[7c4c2892-b369-424f-8372-a0976d15bbf1] railties (6.0.4) lib/rails/rack/logger.rb:37:in `call_app'
[7c4c2892-b369-424f-8372-a0976d15bbf1] railties (6.0.4) lib/rails/rack/logger.rb:26:in `block in call'
[7c4c2892-b369-424f-8372-a0976d15bbf1] activesupport (6.0.4) lib/active_support/tagged_logging.rb:80:in `block in tagged'
[7c4c2892-b369-424f-8372-a0976d15bbf1] activesupport (6.0.4) lib/active_support/tagged_logging.rb:28:in `tagged'
[7c4c2892-b369-424f-8372-a0976d15bbf1] activesupport (6.0.4) lib/active_support/tagged_logging.rb:80:in `tagged'
[7c4c2892-b369-424f-8372-a0976d15bbf1] railties (6.0.4) lib/rails/rack/logger.rb:26:in `call'
[7c4c2892-b369-424f-8372-a0976d15bbf1] actionpack (6.0.4) lib/action_dispatch/middleware/remote_ip.rb:81:in `call'
[7c4c2892-b369-424f-8372-a0976d15bbf1] actionpack (6.0.4) lib/action_dispatch/middleware/request_id.rb:27:in `call'
[7c4c2892-b369-424f-8372-a0976d15bbf1] rack (2.2.3) lib/rack/method_override.rb:24:in `call'
[7c4c2892-b369-424f-8372-a0976d15bbf1] rack (2.2.3) lib/rack/runtime.rb:22:in `call'
[7c4c2892-b369-424f-8372-a0976d15bbf1] activesupport (6.0.4) lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
[7c4c2892-b369-424f-8372-a0976d15bbf1] actionpack (6.0.4) lib/action_dispatch/middleware/executor.rb:14:in `call'
[7c4c2892-b369-424f-8372-a0976d15bbf1] rack (2.2.3) lib/rack/sendfile.rb:110:in `call'
[7c4c2892-b369-424f-8372-a0976d15bbf1] actionpack (6.0.4) lib/action_dispatch/middleware/host_authorization.rb:82:in `call'
[7c4c2892-b369-424f-8372-a0976d15bbf1] railties (6.0.4) lib/rails/engine.rb:527:in `call'
[7c4c2892-b369-424f-8372-a0976d15bbf1] puma (3.12.6) lib/puma/configuration.rb:227:in `call'
[7c4c2892-b369-424f-8372-a0976d15bbf1] puma (3.12.6) lib/puma/server.rb:706:in `handle_request'
[7c4c2892-b369-424f-8372-a0976d15bbf1] puma (3.12.6) lib/puma/server.rb:476:in `process_client'
[7c4c2892-b369-424f-8372-a0976d15bbf1] puma (3.12.6) lib/puma/server.rb:334:in `block in run'
[7c4c2892-b369-424f-8372-a0976d15bbf1] puma (3.12.6) lib/puma/thread_pool.rb:135:in `block in spawn_thread'
I, [2021-09-12T15:44:41.937094 #99155] INFO -- : [6d0d2df9-6526-4496-94aa-ae73ebcc89be] Started GET "/" for 127.0.0.1 at 2021-09-12 15:44:41 -0700
I, [2021-09-12T15:44:41.949275 #99155] INFO -- : [6d0d2df9-6526-4496-94aa-ae73ebcc89be] Processing by HomeController#index as HTML
I, [2021-09-12T15:44:41.951191 #99155] INFO -- : [6d0d2df9-6526-4496-94aa-ae73ebcc89be] Rendering home/index.html.erb within layouts/application
I, [2021-09-12T15:44:41.951576 #99155] INFO -- : [6d0d2df9-6526-4496-94aa-ae73ebcc89be] Rendered home/index.html.erb within layouts/application (Duration: 0.3ms | Allocations: 135)
I, [2021-09-12T15:44:41.952493 #99155] INFO -- : [6d0d2df9-6526-4496-94aa-ae73ebcc89be] Completed 200 OK in 3ms (Views: 1.7ms | Allocations: 1024)
I, [2021-09-12T15:44:42.005012 #99155] INFO -- : [501b9eb7-d4c6-49f0-89fc-cc95a846cd12] Started GET "/assets/application-5abc9724d464865c6ee8976394f0a5c56c7146787659b1e3a4a7ab5e5aaa4286.css" for 127.0.0.1 at 2021-09-12 15:44:42 -0700
I, [2021-09-12T15:44:42.016207 #99155] INFO -- : [ec8802fc-a274-4154-893a-4eed2d44565c] Started GET "/packs/js/application-fc1aefbe06067b06814f.js" for 127.0.0.1 at 2021-09-12 15:44:42 -0700
F, [2021-09-12T15:44:42.029960 #99155] FATAL -- : [501b9eb7-d4c6-49f0-89fc-cc95a846cd12]
[501b9eb7-d4c6-49f0-89fc-cc95a846cd12] ActionController::RoutingError (No route matches [GET] "/assets/application-5abc9724d464865c6ee8976394f0a5c56c7146787659b1e3a4a7ab5e5aaa4286.css"):
[501b9eb7-d4c6-49f0-89fc-cc95a846cd12]
[501b9eb7-d4c6-49f0-89fc-cc95a846cd12] actionpack (6.0.4) lib/action_dispatch/middleware/debug_exceptions.rb:36:in `call'
[501b9eb7-d4c6-49f0-89fc-cc95a846cd12] actionpack (6.0.4) lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'
[501b9eb7-d4c6-49f0-89fc-cc95a846cd12] railties (6.0.4) lib/rails/rack/logger.rb:37:in `call_app'
[501b9eb7-d4c6-49f0-89fc-cc95a846cd12] railties (6.0.4) lib/rails/rack/logger.rb:26:in `block in call'
[501b9eb7-d4c6-49f0-89fc-cc95a846cd12] activesupport (6.0.4) lib/active_support/tagged_logging.rb:80:in `block in tagged'
[501b9eb7-d4c6-49f0-89fc-cc95a846cd12] activesupport (6.0.4) lib/active_support/tagged_logging.rb:28:in `tagged'
[501b9eb7-d4c6-49f0-89fc-cc95a846cd12] activesupport (6.0.4) lib/active_support/tagged_logging.rb:80:in `tagged'
[501b9eb7-d4c6-49f0-89fc-cc95a846cd12] railties (6.0.4) lib/rails/rack/logger.rb:26:in `call'
[501b9eb7-d4c6-49f0-89fc-cc95a846cd12] actionpack (6.0.4) lib/action_dispatch/middleware/remote_ip.rb:81:in `call'
[501b9eb7-d4c6-49f0-89fc-cc95a846cd12] actionpack (6.0.4) lib/action_dispatch/middleware/request_id.rb:27:in `call'
[501b9eb7-d4c6-49f0-89fc-cc95a846cd12] rack (2.2.3) lib/rack/method_override.rb:24:in `call'
[501b9eb7-d4c6-49f0-89fc-cc95a846cd12] rack (2.2.3) lib/rack/runtime.rb:22:in `call'
[501b9eb7-d4c6-49f0-89fc-cc95a846cd12] activesupport (6.0.4) lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
[501b9eb7-d4c6-49f0-89fc-cc95a846cd12] actionpack (6.0.4) lib/action_dispatch/middleware/executor.rb:14:in `call'
[501b9eb7-d4c6-49f0-89fc-cc95a846cd12] rack (2.2.3) lib/rack/sendfile.rb:110:in `call'
[501b9eb7-d4c6-49f0-89fc-cc95a846cd12] actionpack (6.0.4) lib/action_dispatch/middleware/host_authorization.rb:82:in `call'
[501b9eb7-d4c6-49f0-89fc-cc95a846cd12] railties (6.0.4) lib/rails/engine.rb:527:in `call'
[501b9eb7-d4c6-49f0-89fc-cc95a846cd12] puma (3.12.6) lib/puma/configuration.rb:227:in `call'
[501b9eb7-d4c6-49f0-89fc-cc95a846cd12] puma (3.12.6) lib/puma/server.rb:706:in `handle_request'
[501b9eb7-d4c6-49f0-89fc-cc95a846cd12] puma (3.12.6) lib/puma/server.rb:476:in `process_client'
[501b9eb7-d4c6-49f0-89fc-cc95a846cd12] puma (3.12.6) lib/puma/server.rb:334:in `block in run'
[501b9eb7-d4c6-49f0-89fc-cc95a846cd12] puma (3.12.6) lib/puma/thread_pool.rb:135:in `block in spawn_thread'
F, [2021-09-12T15:44:42.031569 #99155] FATAL -- : [ec8802fc-a274-4154-893a-4eed2d44565c]
[ec8802fc-a274-4154-893a-4eed2d44565c] ActionController::RoutingError (No route matches [GET] "/packs/js/application-fc1aefbe06067b06814f.js"):
[ec8802fc-a274-4154-893a-4eed2d44565c]
[ec8802fc-a274-4154-893a-4eed2d44565c] actionpack (6.0.4) lib/action_dispatch/middleware/debug_exceptions.rb:36:in `call'
[ec8802fc-a274-4154-893a-4eed2d44565c] actionpack (6.0.4) lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'
[ec8802fc-a274-4154-893a-4eed2d44565c] railties (6.0.4) lib/rails/rack/logger.rb:37:in `call_app'
[ec8802fc-a274-4154-893a-4eed2d44565c] railties (6.0.4) lib/rails/rack/logger.rb:26:in `block in call'
[ec8802fc-a274-4154-893a-4eed2d44565c] activesupport (6.0.4) lib/active_support/tagged_logging.rb:80:in `block in tagged'
[ec8802fc-a274-4154-893a-4eed2d44565c] activesupport (6.0.4) lib/active_support/tagged_logging.rb:28:in `tagged'
[ec8802fc-a274-4154-893a-4eed2d44565c] activesupport (6.0.4) lib/active_support/tagged_logging.rb:80:in `tagged'
[ec8802fc-a274-4154-893a-4eed2d44565c] railties (6.0.4) lib/rails/rack/logger.rb:26:in `call'
[ec8802fc-a274-4154-893a-4eed2d44565c] actionpack (6.0.4) lib/action_dispatch/middleware/remote_ip.rb:81:in `call'
[ec8802fc-a274-4154-893a-4eed2d44565c] actionpack (6.0.4) lib/action_dispatch/middleware/request_id.rb:27:in `call'
[ec8802fc-a274-4154-893a-4eed2d44565c] rack (2.2.3) lib/rack/method_override.rb:24:in `call'
[ec8802fc-a274-4154-893a-4eed2d44565c] rack (2.2.3) lib/rack/runtime.rb:22:in `call'
[ec8802fc-a274-4154-893a-4eed2d44565c] activesupport (6.0.4) lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
[ec8802fc-a274-4154-893a-4eed2d44565c] actionpack (6.0.4) lib/action_dispatch/middleware/executor.rb:14:in `call'
[ec8802fc-a274-4154-893a-4eed2d44565c] rack (2.2.3) lib/rack/sendfile.rb:110:in `call'
[ec8802fc-a274-4154-893a-4eed2d44565c] actionpack (6.0.4) lib/action_dispatch/middleware/host_authorization.rb:82:in `call'
[ec8802fc-a274-4154-893a-4eed2d44565c] railties (6.0.4) lib/rails/engine.rb:527:in `call'
[ec8802fc-a274-4154-893a-4eed2d44565c] puma (3.12.6) lib/puma/configuration.rb:227:in `call'
[ec8802fc-a274-4154-893a-4eed2d44565c] puma (3.12.6) lib/puma/server.rb:706:in `handle_request'
[ec8802fc-a274-4154-893a-4eed2d44565c] puma (3.12.6) lib/puma/server.rb:476:in `process_client'
[ec8802fc-a274-4154-893a-4eed2d44565c] puma (3.12.6) lib/puma/server.rb:334:in `block in run'
[ec8802fc-a274-4154-893a-4eed2d44565c] puma (3.12.6) lib/puma/thread_pool.rb:135:in `block in spawn_thread'
Webpacker.yml(也有 webpack.config.js?):
# Note: You must restart bin/webpack-dev-server for changes to take effect
default: &default
source_path: javascript
source_entry_path: packs
public_root_path: public
public_output_path: packs
cache_path: tmp/cache/webpacker
webpack_compile_output: true
# Additional paths webpack should lookup modules
# ['app/assets', 'engine/foo/app/assets']
additional_paths: []
# Reload manifest.json on all requests so we reload latest compiled packs
cache_manifest: false
# Extract and emit a css file
extract_css: false
static_assets_extensions:
- .jpg
- .jpeg
- .png
- .gif
- .tiff
- .ico
- .svg
- .eot
- .otf
- .ttf
- .woff
- .woff2
extensions:
- .mjs
- .js
- .jsx
- .sass
- .scss
- .css
- .module.sass
- .module.scss
- .module.css
- .png
- .svg
- .gif
- .jpeg
- .jpg
development:
<<: *default
compile: true
# Reference: https://webpack.js.org/configuration/dev-server/
dev_server:
https: false
host: localhost
port: 3035
public: localhost:3035
hmr: false
# Inline should be set to true if using HMR
inline: true
overlay: true
compress: true
disable_host_check: true
use_local_ip: false
quiet: false
pretty: false
headers:
'Access-Control-Allow-Origin': '*'
watch_options:
ignored: '**/node_modules/**'
test:
<<: *default
compile: true
# Compile test packs to a separate directory
public_output_path: packs-test
production:
<<: *default
# Production depends on precompilation of packs prior to booting for performance.
compile: false
# Extract and emit a css file
extract_css: true
# Cache manifest.json for performance
cache_manifest: true
webpack.config.js:
// Generated using webpack-cli https://github.com/webpack/webpack-cli
const path = require('path');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const isProduction = process.env.NODE_ENV == 'production';
const stylesHandler = isProduction ? MiniCssExtractPlugin.loader : 'style-loader';
const config =
entry: './src/index.js',
output:
path: path.resolve(__dirname, 'dist'),
,
devServer:
open: true,
host: 'localhost',
,
plugins: [
// Add your plugins here
// Learn more about plugins from https://webpack.js.org/configuration/plugins/
],
module:
rules: [
test: /\.(js|jsx)$/i,
loader: 'babel-loader',
,
test: /\.css$/i,
use: [stylesHandler, 'css-loader', 'postcss-loader'],
,
test: /\.(eot|svg|ttf|woff|woff2|png|jpg|gif)$/i,
type: 'asset',
,
// Add your rules for custom modules here
// Learn more about loaders from https://webpack.js.org/loaders/
],
,
;
module.exports = () =>
if (isProduction)
config.mode = 'production';
config.plugins.push(new MiniCssExtractPlugin());
resolve:
extensions: ['.js', '.jsx', '.ts', '.tsx']
else
config.mode = 'development';
return config;
;
application.js 位于 (app/assets/javascript/packs):
import Rails from "@rails/ujs"
import Turbolinks from "turbolinks"
import * as ActiveStorage from "@rails/activestorage"
import "channels"
Rails.start()
Turbolinks.start()
ActiveStorage.start()
用于 webpacker on rails 的部分 Dockerfile 命令:
RUN rails webpacker:install
RUN rails webpacker:install:react
RUN gem list
RUN yarn install
RUN bundle exec rake assets:precompile RAILS_ENV=$RAILS_ENV
入口点.sh:
RAILS_ENV=production bundle exec rake db:create
RAILS_ENV=production bundle exec rake db:migrate
RAILS_ENV=production bundle exec rake db:seed
bundle exec rails s -e production
我认为该错误与我的文件结构以及 webpacker/rails 从中获取资产的位置有关。目前在我的 app/assets/javascript 文件夹中做出反应,我有标准组件,还有一个带有 index.html.erb 的视图内的主文件夹,其中为我的主页组件适当列出了 javascript_pack_tag。想知道我在运行 webpack 的入口点中是否遗漏了一些简单的东西,还是只是文件夹配置错误?
如果需要更多信息,请告诉我,运行 rails 6,webpack 5+。提前致谢!
【问题讨论】:
【参考方案1】:您在文件夹 app/assets/javascript
中反应 js,但您的 webpacker 配置 source_path
指向 app/javascript
# config/webpacker.yml
default: &default
source_path: javascript # -> app/javascript
source_entry_path: packs # -> app/javascript/packs
public_output_path: packs
webpacker
将尝试编译所有 app/javascript/packs/*.js
文件,但找不到任何东西(在您的情况下),因此没有 .js(例如:application.js
)输出文件。
当网络打开时,javascript_pack_tag application
会在public_output_path
中寻找application.js
,但看不到。
您可以尝试将源目录从app/javascript
更改为app/assets/javascript
,如下所示
# config/webpacker.yml
default: &default
source_path: app/assets/javascript
source_entry_path: packs
注意:你可以运行bundle exec rails webpacker:compile
来测试webpack编译是否失败。
【讨论】:
以上是关于带有 Webpack 和 Yarn 的 Rails 6 上的 ActionController 路由错误的主要内容,如果未能解决你的问题,请参考以下文章
带有 WebPack 的 Bootstrap4 的 DataTables 没有集成在 Rails 6 中?
Rails 6.1.0 [webpack-cli] TypeError:无法读取未定义的属性“插件”
通过 Webpack 在 Rails 中安装 jQuery 时未定义 $