###前提・実現したいこと
Deviseにて、作成したUserモデルにて以下のvalidateを設定しています。
validates :email, presence: true, uniqueness: true
新規のユーザー作成時にはvalidateがかかるのですが、Userのupdate時にはかかりません。
Userのupdate時にも書けたいと思っています。
###発生している問題・エラーメッセージ
update時にvalidateにかからず登録されてしまいます。
###該当のソースコード
updateメソッド内は、パスワードが入力されていなくても通るようにしてオーバーライドしてあります。
class Users::RegistrationsController < Devise::RegistrationsController before_action :authenticate_user!, only: [:edit, :update] # GET /resource/sign_up def new super end # POST /resource def create super end # GET /resource/edit def edit super end def update self.resource = resource_class.to_adapter.get!(send(:"current_#{resource_name}").to_key) prev_unconfirmed_email = resource.unconfirmed_email if resource.respond_to?(:unconfirmed_email) # 変更前 if update_resource(resource, account_update_params) if resource.update_without_current_password(account_update_params) yield resource if block_given? if is_flashing_format? flash_key = update_needs_confirmation?(resource, prev_unconfirmed_email) ? :update_needs_confirmation : :updated set_flash_message :notice, flash_key end bypass_sign_in resource respond_with resource, location: after_update_path_for(resource) else clean_up_passwords resource respond_with resource end end
###試したこと
以下のコードがemailのvalidateを退けているのか?と思い、消して再度試しましたが、validateにかからず値が更新されます。
def update .. prev_unconfirmed_email = resource.unconfirmed_email if resource.respond_to?(:unconfirmed_email) .. if is_flashing_format? flash_key = update_needs_confirmation?(resource, prev_unconfirmed_email) ? :update_needs_confirmation : :updated set_flash_message :notice, flash_key end end
###補足情報(言語/FW/ツール等のバージョンなど)
Rails 5.0
Ruby 2.3.1
Devise 4.2.0
あなたの回答
tips
プレビュー