devise 自定义手机号登录

user model中配置
validates_uniqueness_of :phone

def email_required?
  false
end
修改user migration文件,给phone添加唯一值限制

add_index :users, :phone,                unique: true  
在config/initializers/devise.rb中配置登录验证的字段
config.authentication_keys = [:phone]
config.case_insensitive_keys = [:phone]
config.strip_whitespace_keys = [:phone]
application_controller中
before_action :configure_permitted_parameters_name, if: :devise_controller? 

 def configure_permitted_parameters_name
      added_attrs = [:phone, :email, :password, :password_confirmation, :remember_me, :inviter]
      devise_parameter_sanitizer.permit :sign_up, keys: added_attrs
      devise_parameter_sanitizer.permit :account_update, keys: added_attrs
    end

 

猜你喜欢

转载自www.cnblogs.com/znsongshu/p/10373941.html
0条评论
添加一条新回复