rails: devise update user without password

廉价感情. 提交于 2019-12-06 11:54:42
hellomello

I just found out that I've been overriding my devise registration incorrectly.

This was the correct way of having registration controller

Rails 3 /devise seems to ignore custom controller

You could handle that in your model normally. Just edit your valdiations. This way you normally do not need to override the whole controller..just edit the model:

validates :password, :presence => true, :on => :create

Try to change

if @user.update_attributes(params[:user])

to device method

if @user.update_without_password(params[:user])

In class RegistrationsController < Devise::RegistrationsController you are missing update parameters for devise.

def configure_permitted_parameters
  devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:email, :password, ...) }
end

def update
# Get all params for :account_update
account_update_params = devise_parameter_sanitizer.sanitize(:account_update)

# Allow user to update without using password.
if account_update_params[:password].blank?
  account_update_params.delete("password")
  account_update_params.delete("password_confirmation")
end

# Set current_user
@user = User.find(current_user.id)
 if @user.update_attributes(account_update_params)
   set_flash_message :notice, :updated
   sign_in @user, :bypass => true
   redirect_to after_update_path_for(@user)
 else
   render "edit"
 end
end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!