简体   繁体   中英

Rails Devise, No route matches '/users/sign_out' -> SignOut/Logout Routes problem

I have a app using devise to login/out, and view/create profiles. As of yet users may create and delete profiles although there is only supposed to be one profile per user. I have set up my routes just about without problems until comes the SignOut/LogOut from the whole app. Ultimately the error log is all I can decipher, and seems that routes.rb needs some modification for this to work, but I am stumped. Here are the errors and routes.rb: /log/production.log:

Started DELETE "/users/sign_out" for 127.0.0.1 at 2020-01-26 01:56:53 -0500
ActionController::RoutingError (No route matches [DELETE] "/users/sign_out"):

routes.rb

Rails.application.routes.draw do
devise_for :users, :controllers => {:sessions => "users/sessions" }

resources :profiles, only: [:new, :create, :edit, :update, :destroy]

devise_scope :user do
authenticated :user do
root to: 'profiles#index', as: :authenticated_root
get '/profiles/new' => 'profiles#new'
match '/profiles' => 'profiles#create', via: [:get, :post]
get '/profiles/:id' => 'profiles#show'
get '/profiles/:id/edit' => 'profiles#edit'
match '/profiles/:id' => 'profiles#update', via: [:get, :post]
delete '/profiles' => 'profiles#destroy', via: [:get, :post]
end
unauthenticated :user do
root to: 'devise/sessions#new', as: :unauthenticated_root
match '/users/sign_in' => 'devise/sessions#create', via: [:get, :post]
delete '/users/sign_out' => 'devise/sessions#destroy'
end
end
end

I read that using resources :users may affect devise sessions controller, in that I would need a UsersController, however haven't included resources :users in my routes, and/or for a similar error.

You placed the sign-out route in the unauthenticated block in your routes.rb

unauthenticated :user do
  # ..
  delete '/users/sign_out' => 'devise/sessions#destroy'
end

What doesn't makes sense, only authenticated users can sign out. Just move that method into the authenticated :user block above:

authenticated :user do
  # ..
  delete '/users/sign_out' => 'devise/sessions#destroy'
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM