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

后端 未结 2 1850
夕颜
夕颜 2021-01-26 19:05

I\'m running Rails 5.1.2 and Ruby 2.4.0.

I\'ve created a method in the ApplicationController called require_login that checks if the user is logged in order

相关标签:
2条回答
  • 2021-01-26 19:32

    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

    0 讨论(0)
  • 2021-01-26 19:48

    You've done everything right. The only missing thing is the :method => :delete call. This is rails specific and you need rails-ujs or for older rails version jquery_ujs to make it work.

    Add this to your app/assets/javascripts/application.js:

    # for rails 5.1 or higher
    //= require rails-ujs
    
    # for rails 5.0 or lower
    //= require jquery
    //= require jquery_ujs
    

    If you don't add this, the method: :delete call will not work. If you don't want to use it at all, then change sign_out to work with GET.

    Change or add this to your config/initializers/devise.rb:

    config.sign_out_via = :get
    

    If it's still not working, then you have the wrong order in your config/routes.rb file. devise_for :users must be before resources :users:

    devise_for :users
    resources :users
    
    0 讨论(0)
提交回复
热议问题