Rails - Devise, how can I disable some default routes?
Solution 1
Katz's solution no longer works as noted by Cirulli.
Try the following.
devise_for :users, :skip => [:sessions]
as :user do
get "/admin" => "devise/sessions#new", :as => :new_user_session
post "/admin" => "devise/sessions#create", :as => :user_session
end
Solution 2
You can achieve this by using the :skip option to devise_for:
devise_for :users, :skip => [:sessions] do
get "/admin" => "devise/sessions#new", :as => :new_user_session
post "/admin" => "devise/sessions#create", :as => :user_session
end
When I run rake routes after that, I get just:
new_user_session GET /admin(.:format) {:controller=>"devise/sessions", :action=>"new"}
user_session POST /admin(.:format) {:controller=>"devise/sessions", :action=>"create"}
Solution 3
Here it is
devise_for :users, skip: [:sessions,:registrations], controllers: {
omniauth_callbacks: "users/omniauth_callbacks"
}
Solution 4
when you skip sessions
controller, you most add destroy
action to your custom routes too:
as :user do
get "/admin" => "devise/sessions#new", :as => :new_user_session
post "/admin" => "devise/sessions#create", :as => :user_session
delete "/logout" => "devise/sessions#destroy", :as => :destroy_user_session
end
Related videos on Youtube
JohnDel
Software engineer from Greece. Prefered web weapon of choice: Ruby on Rails
Updated on June 07, 2022Comments
-
JohnDel almost 2 years
I want to completely disable the routes /users/sign_in for get and post.
I was able to successfully override them using the following:
devise_for :users do get "/admin" => "devise/sessions#new", :as => :new_user_session post "/admin" => "devise/sessions#create", :as => :user_session end
And when I run rake routes I see the following:
new_user_session GET /admin(.:format) {:controller=>"devise/sessions", :action=>"new"} user_session POST /admin(.:format) {:controller=>"devise/sessions", :action=>"create"} new_user_session GET /users/sign_in(.:format) {:action=>"new", :controller=>"devise/sessions"} POST /users/sign_in(.:format) {:action=>"create", :controller=>"devise/sessions"}
I can access the sign in from /admin as well as from /users/sign_in. But I want to completely remove the last two rows, is it possible?
I tried some different combinations from the documentation which seems to do it but it also overrides some useful ones, like the password/new and password/edit routes.
-
JohnDel over 12 yearsThank you so much for answering my question! I also added this delete "/logout" => "devise/sessions#destroy", :as => :destroy_user_session for the logout and the functionality is exactly what I wanted. :)
-
Gabriele Cirulli over 10 yearsDoesn't seem to work anymore.
devise_for
looks like it's ignoring the block.