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

后端 未结 2 1862
夕颜
夕颜 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: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
    

提交回复
热议问题