10
12

More than 5 years have passed since last update.

devise の Controller のカスタマイズ

Last updated at Posted at 2014-07-08

Deviseをインストールしてもそのコントローラは生成されません。Deviseが内部的に持っているコントローラが使用されます。そのため、Deviseのコントローラの挙動を変えたい場合は、そのDeviseのコントローラクラスを継承した独自のコントローラを作成する必要があります。認証用のモデルを「User」という名前にした場合、app/controllers/usersディレクトリ配下にsessions_controller.rbとregistrations_controller.rbという2つのコントローラファイルを作成します。前者はユーザのログイン/ログアウト機能のコントローラで、後者はユーザ登録機能のコントローラになります。それぞれソースコードは以下のようにします。

sessions_controller.rb
class Users::SessionsController < Devise::SessionsController

  def new
    super
  end

  def create
    super
  end
end

original の sessions_controller.rb
https://raw.githubusercontent.com/plataformatec/devise/master/app/controllers/devise/sessions_controller.rb

registrations_controller.rb
class Users::RegistrationsController < Devise::RegistrationsController

  def new
    super
  end

  def create
    super
  end

end

original の registrations_controller.rb
https://raw.githubusercontent.com/plataformatec/devise/master/app/controllers/devise/registrations_controller.rb

10
12
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
10
12