0
0

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 1 year has passed since last update.

deviseの特定のアクションをカスタマイズする

0
Posted at

はじめに

deviseを使っていて特定のアクションをカスタマイズしたい場面は結構あると思います。
その時のメモです。

環境

rails 6.1.6.7
ruby 3.0.6
devise 4.9.3

状況

私の場合、deviseにconfirmableな設定を追加していました。
ユーザー登録時に追加のアクションをしたかったので、registrations_controller.rbのcreateアクションをカスタマイズすることになります。

カスタマイズをするにあたりdeviseのコードを読んでみるのは非常に参考になりました。
https://github.com/heartcombo/devise/tree/main

route編集

registrations_controllerを編集するときは明示的に登録系の処理にはusers/registrations_controllerを使うように指定する必要があります。

config/routes.rb
Rails.application.routes.draw do
  devise_for :users, controllers: {
    registrations: 'users/registrations',
  }
end

アクションカスタマイズ

deviseのユーザー新規登録の時はregistrationsコントローラーのcreateアクションが実行されます。

app/controllers/users/registrations_controller.rb
class Users::RegistrationsController < Devise::RegistrationsController
  def create
    binding.pry
    super
  end
end

上記のようにすれば登録時に処理の止めて実際にどんなデータが送られているのか確認することができます。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?