LoginSignup
3
3

More than 5 years have passed since last update.

No route matches [DELETE] "/users/sign_out" エラー(Rails)

Posted at

はじめに

初学者の学習アウトプット用記事です。
ご指摘訂正アドバイスは是非お願いします。

で、今回のテーマは!!!!!!

エラー!!!!です。

内容としては、deviseでログイン機能を実装してログインはできるのにログアウトするとエラーが起きてしまうということでした。

No route matches [DELETE] "/users/sign_out"

エラー文が

No route matches [DELETE] "/users/sign_out"

なんとなくなんですが、

「DELETEのルーティングされてねぇぞ!!ごらぁああ!!!!」

と言われてる気がしました。

ターミナルにてrake routesコマンド実行してみると、

destroy_user_session  GET    /users/sign_out(.:format)            devise/sessions#destroy 

#メソッドがGETになってる???

ビューファイルのログアウトボタンのリンクのパスは問題なく記述できてました。
sign_outログアウトのメソッドってDELETEじゃないの???????

ってことでルーティング確認!!

routes.rb

Rails.application.routes.draw do
  devise_for :users
  root 'groups#index'

  resources :users, only: [:index,:edit, :update]
  resources :groups, only: [:new, :create, :edit, :update] do
    resources :messages, only: [:index, :create]
   end
end

あれ???問題なくね????

どこがおかしいんだ?・・・・・

ってことでメンターさんに質問。

メンターさんも??????あれ????なんだろう???とrake routesで確認。
するとやはりGETになってる。

試しにroutes.rbファイルのdevise_for :usersの記述を削除

routes.rb
Rails.application.routes.draw do

  root 'groups#index'

  resources :users, only: [:index,:edit, :update]
  resources :groups, only: [:new, :create, :edit, :update] do
    resources :messages, only: [:index, :create]
  end
end

そしてターミナルでrake routesコマンド!!!!

すると!!!!!

destroy_user_session DELETE /users/sign_out(.:format)            devise/sessions#destroy

GETがDELETEに変わってる!!!!

そのまま再度、devise_for :usersを記述。

routes.rb
Rails.application.routes.draw do
  devise_for :users
  root 'groups#index'

  resources :users, only: [:index,:edit, :update]
  resources :groups, only: [:new, :create, :edit, :update] do
    resources :messages, only: [:index, :create]
   end
end

そんで再度ターミナルでrake routesで確認したらDELETEになっていました。

無事、その後rails sでサーバー再起動させたら問題なくログアウトできました。

とりあえず解決はできたのですが、
今回の件はバグ?みたいなものと説明されましたが真相は謎です。

他にも

記事を調べたら

[Rails 4.x] Devise で Sign Out が Routing Error になる際の対応。(method の delete が get になる場合)
https://qiita.com/colorrabbit/items/5545fce7e5cd4e494396

の記事がでてきました。
上記の記事の内容を試したのですがこちらは特に変化がなくエラーのままでした。

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3