29
25

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Rails】deviseを使用してroutes.rbでresources :usersするときは順番に気をつける

Last updated at Posted at 2019-03-19

#事象

rails g devise User を実装して、users controllerにはbefore_action :authenticate_user!してある。

users_controller.rb
class UsersController < ApplicationController
  before_action :authenticate_user!
  def index
  end

  def show
  end
end
routes.rb
Rails.application.routes.draw do
  root 'home#index'
  resources :users
  devise_for :users
end

rootがHome#indexで、ここからログイン、新規アカウント作成へリンクしている。

スクリーンショット 2019-03-19 18.51.12.png

しかし、 ログインをクリックするとログインページへは移動せずに「このページは動作していません」となってしまった。

スクリーンショット 2019-03-19 18.51.21.png

原因と解決方法

ログを見ると Parameters: {"id"=>"sign_in"} となっていて、Completed 401 Unauthorized が返っているのがわかる。

Started GET "/users/sign_in" for 127.0.0.1 at 2019-03-20 00:42:44 +0900
Processing by UsersController#show as HTML
  Parameters: {"id"=>"sign_in"}
Completed 401 Unauthorized in 1ms (ActiveRecord: 0.0ms)

 

rails routes を見てみると。

$ rails routes
                   Prefix Verb   URI Pattern                                                                              Controller#Action
                     root GET    /                                                                                        home#index
                    users GET    /users(.:format)                                                                         users#index
                          POST   /users(.:format)                                                                         users#create
                 new_user GET    /users/new(.:format)                                                                     users#new
                edit_user GET    /users/:id/edit(.:format)                                                                users#edit
                     user GET    /users/:id(.:format)                                                                     users#show
                          PATCH  /users/:id(.:format)                                                                     users#update
                          PUT    /users/:id(.:format)                                                                     users#update
                          DELETE /users/:id(.:format)                                                                     users#destroy
         new_user_session GET    /users/sign_in(.:format)                                                                 devise/sessions#new
             user_session POST   /users/sign_in(.:format)                                                                 devise/sessions#create
     destroy_user_session DELETE /users/sign_out(.:format)                                                                devise/sessions#destroy
        new_user_password GET    /users/password/new(.:format)                                                            devise/passwords#new
       edit_user_password GET    /users/password/edit(.:format)                                                           devise/passwords#edit
            user_password PATCH  /users/password(.:format)                                                                devise/passwords#update
                          PUT    /users/password(.:format)                                                                devise/passwords#update
                          POST   /users/password(.:format)                                                                devise/passwords#create
 cancel_user_registration GET    /users/cancel(.:format)                                                                  devise/registrations#cancel
    new_user_registration GET    /users/sign_up(.:format)                                                                 devise/registrations#new
   edit_user_registration GET    /users/edit(.:format)                                                                    devise/registrations#edit
        user_registration PATCH  /users(.:format)                                                                         devise/registrations#update
                          PUT    /users(.:format)                                                                         devise/registrations#update
                          DELETE /users(.:format)                                                                         devise/registrations#destroy
                          POST   /users(.:format)                                                                         devise/registrations#create

となっており、

/users/:id(.:format)

/users/sign_in(.:format)
より上位(優先度が上)にいるのが原因だった。

なのでroutes.rbを

routes.rb
Rails.application.routes.draw do
  root 'home#index'
  devise_for :users
  resources :users
end

にすれば無事ログイン画面へ遷移できた。
(devise_for :users を上、resources :usersを下にした)

スクリーンショット 2019-03-20 1.02.52.png (例のログイン画面)
29
25
1

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
29
25

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?