【发布时间】:2014-05-07 06:34:03
【问题描述】:
我正在使用 Rails 4 和 Devise 3.2.4 进行身份验证。
我正在尝试允许用户在不提供密码的情况下更新他们的帐户(例如:姓名、电子邮件...等)。
我已按照本教程进行操作-> https://github.com/plataformatec/devise/wiki/How-To:-Allow-users-to-edit-their-account-without-providing-a-password
按照教程所说的,我可以在不提供密码的情况下更新用户,但是当我想更改“密码”本身时,我不需要提供密码确认和当前密码。
如何允许用户在不提供密码的情况下更改名字,姓氏,并且在更改密码时,用户需要输入密码,密码确认和当前密码。
请看下面我的代码。
谢谢。
===更新==
如果我的User Model 中有:validatable,我的代码似乎不起作用
==
registrations_controller.rb
class RegistrationsController < Devise::RegistrationsController
def update
account_update_params = devise_parameter_sanitizer.sanitize(:account_update)
if account_update_params[:password].blank?
account_update_params.delete("password")
account_update_params.delete("password_confirmation")
end
@user = User.find(current_user.id)
if @user.update_attributes(devise_parameter_sanitizer.sanitize(:account_update))
set_flash_message :notice, :updated
sign_in @user, :bypass => true
redirect_to @user
else
render "edit"
end
end
protected
end
application_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_filter :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:account_update) { |u|
u.permit(:firstname, :lastname, :password, :password_confirmation )}
end
end
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-4 devise