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?

More than 1 year has passed since last update.

devise nameカラム追加<忘備録>

Posted at

deviseにはデフォルトでnameカラムがない。
そのため、こちらで追加する必要がある。

基本的なことなので、大丈夫かと思いつつ忘備録。

migrationファイルの作成

rails g migration AddNameToUser name:string

テーブルに反映

rails db:migrate

次にバリデーションの追記

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable

validates :name, presence: true, length: { maximum: 50 }
end

以下のようにストロングパラメターでnameを受け取れるように設定をデフォルトから変更する必要がある。デフォルトはメールアドレスとパスワード。

app/controllers/application_controller.rb
class ApplicationController < ActionController::Base

  protect_from_forgery with: :exception

  before_action :configure_permitted_parameters, if: :devise_controller?

  protected

    def configure_permitted_parameters
      devise_parameter_sanitizer.permit(:sign_up, keys: [:name])
      devise_parameter_sanitizer.permit(:account_update, keys: [:name])
    end
end
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?