Reputation: 15
I am building an app in Ruby on Rails 7 that has Admin and User side. after configuring Devise, all of my Devise routes redirect to "home#index". how can I fix this? I inititially thought it was because there were no Devise views in my /app/views but after running rails generate devise:views, and having the views created, the problem still persits. PLease assist
this is my routes.rb file and below that is my server logs
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)
Upvotes: 2
Views: 88
Reputation: 30061
In your logs you have:
Filter chain halted as :require_no_authentication rendered or redirected
that means you're already signed in and you get redirected back to root
path. You should also set up flash
messages in your layout so you could see the notice devise gives you.
Second issue is your routes:
# this needs to come first to override home root when signed in
authenticated :admin do
root to: "admin#index", as: :admin_root
end
root "home#index"
Upvotes: 0