Rails7中Devise管理员登录路由重定向至首页问题求助
解决Rails 7中Devise路由重定向至home#index的问题
我正在构建一个包含Admin和User端的Ruby on Rails 7应用。配置Devise后,访问Devise路由(如/admins/sign_in)会被重定向至home#index。起初以为是缺少Devise视图,执行rails generate devise:views创建视图后问题仍未解决。以下是我的routes.rb和服务器日志:
Rails.application.routes.draw do devise_for :admins # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html # Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500. # Can be used by load balancers and uptime monitors to verify that the app is live. get "up" => "rails/health#show", as: :rails_health_check # Defines the root path route ("/") root "home#index" authenticated :admin_user do root to: "admin#index", as: :admin_root end get "admin" => "admin#index" end
=> Booting Puma => Rails 7.1.3.3 application starting in development => Run `bin/rails server --help` for more startup options Puma starting in single mode... * Puma version: 6.4.2 (ruby 3.3.1-p55) ("The Eagle of Durango") * Min threads: 5 * Max threads: 5 * Environment: development * PID: 14050 * Listening on http://127.0.0.1:3000 * Listening on http://[::1]:3000 Use Ctrl-C to stop Started GET "/" for 127.0.0.1 at 2024-06-01 07:34:29 +0200 ActiveRecord::SchemaMigration Load (0.2ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC Processing by HomeController#index as HTML Rendering layout layouts/application.html.erb Rendering home/index.html.erb within layouts/application Rendered home/index.html.erb within layouts/application (Duration: 1.5ms | Allocations: 286) Rendered layout layouts/application.html.erb (Duration: 339.3ms | Allocations: 78245) Completed 200 OK in 360ms (Views: 347.1ms | ActiveRecord: 0.0ms | Allocations: 82659) Started GET "/admins/sign_in" for 127.0.0.1 at 2024-06-01 07:34:38 +0200 Processing by Devise::SessionsController#new as HTML Admin Load (0.3ms) SELECT "admins".* FROM "admins" WHERE "admins"."id" = ? ORDER BY "admins"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]] Redirected to http://localhost:3000/ Filter chain halted as :require_no_authentication rendered or redirected Completed 302 Found in 70ms (ActiveRecord: 7.4ms | Allocations: 17165) Started GET "/" for 127.0.0.1 at 2024-06-01 07:34:38 +0200 Processing by HomeController#index as HTML Rendering layout layouts/application.html.erb Rendering home/index.html.erb within layouts/application Rendered home/index.html.erb within layouts/application (Duration: 0.2ms | Allocations: 37) Rendered layout layouts/application.html.erb (Duration: 35.1ms | Allocations: 8276) Completed 200 OK in 37ms (Views: 36.3ms | ActiveRecord: 0.0ms | Allocations: 8535) Started GET "/" for 127.0.0.1 at 2024-06-01 07:35:12 +0200 Processing by HomeController#index as HTML Rendering layout layouts/application.html.erb Rendering home/index.html.erb within layouts/application Rendered home/index.html.erb within layouts/application (Duration: 0.8ms | Allocations: 159) Rendered layout layouts/application.html.erb (Duration: 65.8ms | Allocations: 25157) Completed 200 OK in 70ms (Views: 68.5ms | ActiveRecord: 0.0ms | Allocations: 26088) Started GET "/admin/sign_in" for 127.0.0.1 at 2024-06-01 07:35:20 +0200 ActionController::RoutingError (No route matches [GET] "/admin/sign_in"): Started GET "/admins/sign_in" for 127.0.0.1 at 2024-06-01 07:35:32 +0200 Processing by Devise::SessionsController#new as HTML Admin Load (0.2ms) SELECT "admins".* FROM "admins" WHERE "admins"."id" = ? ORDER BY "admins"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]] Redirected to http://localhost:3000/ Filter chain halted as :require_no_authentication rendered or redirected Completed 302 Found in 22ms (ActiveRecord: 8.1ms | Allocations: 5881) Started GET "/" for 127.0.0.1 at 2024-06-01 07:35:32 +0200 Processing by HomeController#index as HTML Rendering layout layouts/application.html.erb Rendering home/index.html.erb within layouts/application Rendered home/index.html.erb within layouts/application (Duration: 0.2ms | Allocations: 37) Rendered layout layouts/application.html.erb (Duration: 32.8ms | Allocations: 8408) Completed 200 OK in 35ms (Views: 34.5ms | ActiveRecord: 0.0ms | Allocations: 8662) Started GET "/admins/sign_in" for 127.0.0.1 at 2024-06-01 07:44:14 +0200 Processing by Devise::SessionsController#new as HTML Admin Load (1.9ms) SELECT "admins".* FROM "admins" WHERE "admins"."id" = ? ORDER BY "admins"."id" ASC LIMIT ? [["id", 1], ["LIMIT", 1]] Redirected to http://localhost:3000/ Filter chain halted as :require_no_authentication rendered or redirected Completed 302 Found in 43ms (ActiveRecord: 2.9ms | Allocations: 5332) Started GET "/" for 127.0.0.1 at 2024-06-01 07:44:14 +0200 Processing by HomeController#index as HTML Rendering layout layouts/application.html.erb Rendering home/index.html.erb within layouts/application Rendered home/index.html.erb within layouts/application (Duration: 3.5ms | Allocations: 157) Rendered layout layouts/application.html.erb (Duration: 570.5ms | Allocations: 26921) Completed 200 OK in 585ms (Views: 580.6ms | ActiveRecord: 0.0ms | Allocations: 27850)
问题分析
从日志中可以看到两个关键问题:
- 已登录Admin用户导致重定向:每次访问
/admins/sign_in时,日志都会加载id=1的Admin记录,说明当前会话中已有已认证的Admin用户。Devise的require_no_authentication过滤器会自动阻止已登录用户访问登录页面,重定向到默认根路径home#index。 - 路由配置错误:
routes.rb中的authenticated :admin_user是错误的,应为:admin(对应你的Devise模型Admin)。
解决步骤
1. 清除已登录的Admin会话
通过以下方式之一清除登录状态:
- 关闭浏览器并清除缓存/会话数据
- 在Rails控制台中删除当前登录的Admin用户:
# 执行rails c进入控制台 Admin.find(1).destroy - 执行数据库重置(仅开发环境使用,会清空所有数据):
rails db:reset
2. 修正routes.rb中的认证路由
将authenticated :admin_user改为authenticated :admin,修正后的代码如下:
Rails.application.routes.draw do devise_for :admins get "up" => "rails/health#show", as: :rails_health_check # 未认证用户的根路径 root "home#index" # 已认证Admin用户的根路径 authenticated :admin do root to: "admin#index", as: :admin_root end get "admin" => "admin#index" end
3. 验证正确的路由
执行以下命令查看所有Devise生成的Admin路由:
rails routes | grep admin
确保访问正确的登录路由(应为/admins/sign_in,而非/admin/sign_in,后者会触发路由错误)。
内容的提问来源于stack exchange,提问作者Mambo Zulu
相关产品推荐
相关产品推荐

