1
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?

【rails】devise で独自のカラムを追加する方法

Last updated at Posted at 2025-05-26

はじめに

こんにちは。アメリカ在住で、独学でエンジニアを目指している Taira です。
現在 Rails アプリを devise を用いて作成しています。
前回は devise の導入方法についての記事を書きましたが、devise を使うことであらかじめ emailencrypted_password などのカラムが与えられます。
しかし、name や username 等の独自カラムを追加したい場面も多いでしょう。
そこで本日は devise で独自カラムを追加する方法について解説します。

前提

まず、以下のコマンドで devise の user モデルを作成します。

bin/rails generate devise User

このコマンドで User モデルとマイグレーションファイルが自動生成されます。

生成された db/migrate/TIMESTAMP_devise_create_users.rb を開き、name や username など追加したいカラムを追記します。

class DeviseCreateUsers < ActiveRecord::Migration[8.0]
  def change
    create_table :users do |t|
      ## Database authenticatable
      t.string :name, null: false # 追加
      t.string :email,              null: false, default: ""
      t.string :encrypted_password, null: false, default: ""
      # ...(省略)
      t.timestamps null: false
    end
    # ...(省略)
  end
end

上記に name を追加したら、以下でマイグレーションを適用します。

bin/rails db:migrate

正常に追加できていれば、db/schema.rb の users テーブルに name が含まれていることを確認してください。

create_table "users", force: :cascade do |t|
  t.string "name", null: false # 追加されていることを確認
  # ...(省略)
end

このままだと何が問題なのか?

devise のコントローラーは初期状態では emailpassword しか許可していません。
そのため、signup 画面で name を送信しても保存されません。
独自カラム(例: name)をフォームから保存できるようにするには strong parameters の追加設定が必要です。

ここでは signup(registrations コントローラ)への追加方法を紹介します。

Devise コントローラーのカスタマイズ

まず devise のコントローラーを生成します。

rails generate devise:controllers users

次に app/controllers/application_controller.rbconfigure_permitted_parameters メソッドを追加します。

class ApplicationController < ActionController::Base
  before_action :configure_permitted_parameters, if: :devise_controller?

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up, keys: [:name])
  end
end

configure_permitted_parametersはストロングパラメータを指しており、deviseで自分で作成したカラムを追加してサインインの時に使用したいときはapplication_controller.rbにまとめて書いておくことが一般的なようです。

動作確認

これでサインアップ時やプロフィール編集時に name カラムが保存されるようになります。
画面や rails console で name が登録できているか確認しましょう。

参考資料

1
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
1
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?