0
0

More than 1 year has passed since last update.

【Rails】devise 管理者が他のユーザーの新規登録ができるようにする

Posted at

【Rails】devise 管理者が他のユーザーの新規登録ができるようにする

devise

deviseを使用してユーザー登録機能を実装した際に、管理者としてログインをし他のユーザーの新規登録ができるようにしたいとします。
しかし、そのままの状態で新規登録をしようとするとroot_pathにリダイレクトされてしまい新規作成画面に遷移できません。

rails g devise:controllers users

本来deviseのコントローラーはGem内に記述されているため編集することはできません。
しかしdeviseのユーザー登録機能はregistrations_controller.rbで定義されているため、このファイルをどうにかしなくてはいけません。
そこで、このcontrollerをオーバーライドしてカスタマイズをしていきます。

まずはオーバーライドするためのファイルを作成します。

$ rails g controller users/registrations

これで編集するファイルapp/controllers/users/registrations_controller.rbが作成できます。

それではこのファイルを編集していきます。

registrations_controller.rb
class Users::RegistrationsController < Devise::RegistrationsController
    prepend_before_filter :require_no_authentication, :only => [ :cancel]
    prepend_before_filter :authenticate_scope!, :only => [:new, :create ,:edit, :update, :destroy]
          (省略)
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