Rails 在生产模式下使用 Devise 没有路由匹配 [GET] “/users/sign_out”

Rails with Devise in production mode No route matches [GET] "/users/sign_out"

我知道这个问题已经被问过几次了。但我已经尝试了在其他线程上讨论的所有解决方案,其中 none 对我有用。

我正在使用 rails v5.2.1ruby v2.4.1 with devise

我的应用程序在开发模式下运行得非常好。一旦我切换到生产环境,我的注销 link 就会失败 No route matches [GET] "/users/sign_out"

我的注销 link 应该触发 DELETE 而不是 GET

  <%= link_to('Logout', destroy_user_session_path, method: :delete) %>

这是我的路线

$rake routes | grep  sign_out
destroy_user_session DELETE   /users/sign_out(.:format)                devise/sessions#destroy

我已经使用

预编译了我的资产
 rake assets:clobber RAILS_ENV=production
 rake assets:precompile RAILS_ENV=production

我尝试了以下各种组合的配置选项

# devise.rb
config.sign_out_via = :delete

# production.rb
config.public_file_server.enabled = true
config.assets.compile = true

我是 rails 的新手,这是我第一次创建生产环境。所以我可能犯了一个非常基本的错误,但我还没弄清楚

这是点击 sign_out link

触发的日志的全部内容
INFO -- : [55a64c1a-feb0-45bb-a8e8-5e3bf5efe461] Started GET "/users/sign_out" for 127.0.0.1 at 2018-09-15 17:02:20 +0200
FATAL -- : [55a64c1a-feb0-45bb-a8e8-5e3bf5efe461]   
FATAL -- : [55a64c1a-feb0-45bb-a8e8-5e3bf5efe461] ActionController::RoutingError (No route matches [GET] "/users/sign_out"):
FATAL -- : [55a64c1a-feb0-45bb-a8e8-5e3bf5efe461]   
FATAL -- : [55a64c1a-feb0-45bb-a8e8-5e3bf5efe461] actionpack (5.2.1) lib/action_dispatch/middleware/debug_exceptions.rb:65:in `call'
actionpack (5.2.1) lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'
railties (5.2.1) lib/rails/rack/logger.rb:38:in `call_app'
railties (5.2.1) lib/rails/rack/logger.rb:26:in `block in call'
activesupport (5.2.1) lib/active_support/tagged_logging.rb:71:in `block in tagged'
activesupport (5.2.1) lib/active_support/tagged_logging.rb:28:in `tagged'
activesupport (5.2.1) lib/active_support/tagged_logging.rb:71:in `tagged'
railties (5.2.1) lib/rails/rack/logger.rb:26:in `call'
actionpack (5.2.1) lib/action_dispatch/middleware/remote_ip.rb:81:in `call'
actionpack (5.2.1) lib/action_dispatch/middleware/request_id.rb:27:in `call'
rack (2.0.5) lib/rack/method_override.rb:22:in `call'
rack (2.0.5) lib/rack/runtime.rb:22:in `call'
activesupport (5.2.1) lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
actionpack (5.2.1) lib/action_dispatch/middleware/executor.rb:14:in `call'
rack (2.0.5) lib/rack/sendfile.rb:111:in `call'
railties (5.2.1) lib/rails/engine.rb:524:in `call'
puma (3.12.0) lib/puma/configuration.rb:225:in `call'
puma (3.12.0) lib/puma/server.rb:658:in `handle_request'
puma (3.12.0) lib/puma/server.rb:472:in `process_client'
puma (3.12.0) lib/puma/server.rb:332:in `block in run'
puma (3.12.0) lib/puma/thread_pool.rb:133:in `block in spawn_thread'

无论如何,我的其余页面不断抛出这些关于无法找到我的资产的错误。我似乎也无法清除这些错误。我已经多次重新编译我的资产,也浏览了许多关于它们的不同文档。据我所知它应该工作

INFO -- : [599f3a59-d3b1-4e0b-a974-a27abda77a0e] Started GET 
"/control_panels" for 127.0.0.1 at 2018-09-15 17:13:15 +0200
INFO -- : [599f3a59-d3b1-4e0b-a974-a27abda77a0e] Processing by 
ControlPanelsController#index as HTML
DEBUG -- : [599f3a59-d3b1-4e0b-a974-a27abda77a0e]   [1m[36mUser Load (1.0ms)[0m  [1m[34mSELECT  "users".* FROM "users" WHERE "users"."id" =  ORDER BY "users"."id" ASC LIMIT [0m  [["id", 980190963], ["LIMIT", 1]]
INFO -- : [599f3a59-d3b1-4e0b-a974-a27abda77a0e]   Rendering control_panels/index.html.erb within layouts/application
INFO -- : [599f3a59-d3b1-4e0b-a974-a27abda77a0e]   Rendered control_panels/_index.erb (1.2ms)
INFO -- : [599f3a59-d3b1-4e0b-a974-a27abda77a0e]   Rendered control_panels/index.html.erb within layouts/application (1.6ms)
INFO -- : [599f3a59-d3b1-4e0b-a974-a27abda77a0e] Completed 200 OK in 25ms (Views: 3.9ms | ActiveRecord: 1.0ms)
INFO -- : [efbfb389-6227-4e34-bd72-f4febeecf5c2] Started GET "/assets/application-35729bfbaf9967f119234595ed222f7ab14859f304ab0acc5451afb387f637fa.css" for 127.0.0.1 at 2018-09-15 17:13:15 +0200
 FATAL -- : [efbfb389-6227-4e34-bd72-f4febeecf5c2]   
 FATAL -- : [efbfb389-6227-4e34-bd72-f4febeecf5c2] ActionController::RoutingError (No route matches [GET] "/assets/application-35729bfbaf9967f119234595ed222f7ab14859f304ab0acc5451afb387f637fa.css"):
 FATAL -- : [efbfb389-6227-4e34-bd72-f4febeecf5c2]   
 FATAL -- : [efbfb389-6227-4e34-bd72-f4febeecf5c2] actionpack (5.2.1) 
 lib/action_dispatch/middleware/debug_exceptions.rb:65:in `call'
 actionpack (5.2.1) 
 lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'
 railties (5.2.1) lib/rails/rack/logger.rb:38:in `call_app'
 railties (5.2.1) lib/rails/rack/logger.rb:26:in `block in call'
 activesupport (5.2.1) lib/active_support/tagged_logging.rb:71:in 
`block in tagged'
 activesupport (5.2.1) lib/active_support/tagged_logging.rb:28:in 
`tagged'
 activesupport (5.2.1) lib/active_support/tagged_logging.rb:71:in 
`tagged'
 railties (5.2.1) lib/rails/rack/logger.rb:26:in `call'
 actionpack (5.2.1) lib/action_dispatch/middleware/remote_ip.rb:81:in 
`call'
 actionpack (5.2.1) lib/action_dispatch/middleware/request_id.rb:27:in `call'
 rack (2.0.5) lib/rack/method_override.rb:22:in `call'
 rack (2.0.5) lib/rack/runtime.rb:22:in `call'
 activesupport (5.2.1) 
 lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
 actionpack (5.2.1) lib/action_dispatch/middleware/executor.rb:14:in `call'
 rack (2.0.5) lib/rack/sendfile.rb:111:in `call'
 railties (5.2.1) lib/rails/engine.rb:524:in `call'
 puma (3.12.0) lib/puma/configuration.rb:225:in `call'
 puma (3.12.0) lib/puma/server.rb:658:in `handle_request'
 puma (3.12.0) lib/puma/server.rb:472:in `process_client'
 puma (3.12.0) lib/puma/server.rb:332:in `block in run'
 puma (3.12.0) lib/puma/thread_pool.rb:133:in `block in spawn_thread'```

正如您已经正确指出的那样,路由是针对 DELETE 方法的,因此 GET 请求会触发 404。link_to 助手无法与 [=13] 一起使用=] 选项通常意味着 Javascript 在您的浏览器中被禁用。 为了安全起见,使用 button_to 而不是 link_to.