Okayama.rbでとあるプロジェクトをしているのですが、その最初のほうを自分が作り込むから待っててくれといって早数週間。めっちゃDeviseでハマってました…。
rails-apiを使って、jsonを返すだけのapiサーバーを作ろうとしているので、deviseでのログインはトークンからだけでいい。
なので、config/routes.rbでdevise_forを使わないでもいいだろうと思ってコメントアウトしたらテストが死んでしまったので調査開始。
また、グループウェア的なのを作ろうとしているのだけれど、だれでも登録できるグループウェアだとダメなので、登録機能をオフにするのもどうすればいいんだろう?と思っていたら、自分でオンにしているだけだった…。まず登録機能をオフにするには:registerableをコメントアウトすればいい。
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable,
# :registerable,
:recoverable,
:trackable,
:validatable,
:token_authenticatable
end
これでregistration系のルーティングは消せます。
次に、ログイン(サインイン)用のルーティングを消します。
これはルーティング側に書きました。skip: [:sessions]らしいです。
また、Deviseのルーティングは普通のusersを使ってしまうとユーザー管理のコントローラと被ってしまうので、敢えてこれを変更しておきました。それがpath: :meの部分です。
devise_for :users, path: :me, skip: [:sessions] resources :users, except: [:new, :edit]
これによって設定されたルーティングはこうなりました。
user_password POST /me/password(.:format) devise/passwords#create
new_user_password GET /me/password/new(.:format) devise/passwords#new
edit_user_password GET /me/password/edit(.:format) devise/passwords#edit
PATCH /me/password(.:format) devise/passwords#update
PUT /me/password(.:format) devise/passwords#update
users GET /users(.:format) users#index
POST /users(.:format) users#create
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
パスワード更新用のルーティングと普通のユーザー管理用のルーティングのみ残すことに成功。やったね!
