On Destroying Session via Devise "Couldn't Find User with 'Id'=Sign_Out"

On destroying session via Devise Couldn't find User with 'id'=sign_out

resources :users and devise_for :users are conflict so

try below code:

routes.rb

Rails.application.routes.draw do
root to: 'visitors#index'
devise_for :users
#resources :users //comment the users resources because its already used by devise_for or used some other resources for users

get "home/index"
get "home/minor"
root to: 'home#index'

end

Additionally, in your config/initalizer/devise.rb file

Change config.sign_out_via = :delete to config.sign_out_via = :get

Couldn't find User with id=sign_out

You need to move:

devise_for :users do get '/users/sign_out' => 'devise/sessions#destroy' 

over your devise_scope. Rails is looking for routes from top of Routes file. Your sign out url matches users/:id, hence it is trying to render show action with sign_out being an id.

UPDATE:

Actually, do you really need the last line in your devise_scope block?

Rails/Devise: Couldn't find User with 'id'=sign_out

  1. My application.js file needed the defaulted comments:

    //= require jquery

    //= require jquery_ujs

  2. My layouts/application.haml file needed the CSRF meta tags:

    = csrf_meta_tags

Thanks to sevenseacat for pointing me in the right direction!

Error trying to logout: Couldn't find User with 'id'=sign_out

move get '/users/sign_out' => 'devise/sessions#destroy' above resources :users in your routes. Routes are given priority in terms of their order in the routes file.

devise redirect on login error: ActiveRecord::RecordNotFound in DocumentsController#show

I fixed my problem by following this question.Following is my routes.rb

   resources :users do
resources :documents, only: [:index]
end
resource :documents, except: [:index]

this gives me document#index path as well as document#new path.

devise destroy session path is conflicting with another user path

In your routes file, you've specified that this route should be matched on a DELETE request. However, your signout link is sending a GET request. This is why the request is matching UserControllers#show.

Assuming you're using jquery, you can set up this link to send the signout request as DELETE by making sure you've added jquery_ujs to the included javascript. This will modify the request method for links that have the data-method attribute set to something other than 'GET', To specify the proper :method param for your signout link_to, use:

link_to "Sign out", destroy_user_session_path, method: :delete

Of course, this will only work if your users have Javascript enabled; otherwise, the request will fallback to GET anyways. If you want to make signout work for GET requests, it will need to have higher precedence in your routes file than the show route.

Devise: Why doesn't my logout link work?

The problem lies in the fact that in your logs the signout request is a GET request.

Started GET "/users/sign_out"

But the signout route is a DELETE

destroy_user_session DELETE /users/sign_out(.:format) 

The reason why you are getting the exception is that is it getting confused with one of the routes created by resources :users which would be something like

edit_user GET /users/(:id)(.:format) {:action=>"edit", :controller=>"users"}

Basically 'sign_out' is being mistaken as a id.

I'm not sure why the delete link is not going through as a DELETE request. Though changing

config.sign_out_via = :delete

to be :get might solve the problem.



Related Topics



Leave a reply



Submit