2016-09-24 47 views
0

我使用由设计生成的用户范围控制器传递附加属性。设计问题 - 允许用户编辑没有密码

class Users::RegistrationsController < Devise::RegistrationsController 
before_action :sign_up_params, only: [:create] 
before_action :account_update_params, only: [:update 

protected 

    def sign_up_params 
    devise_parameter_sanitizer.permit(:sign_up, keys: [:first_name, :last_name, :cpf]) 
    end 

    def account_update_params 
    devise_parameter_sanitizer.permit(:account_update, keys: [:first_name, :last_name, :cpf, :birth_date, :phone, :gender]) 
    end 

def update_resource(resource, params) 
    resource.update_without_password(params) 
end 

end 

devise_for :users, controllers: {registrations: 'users/registrations} 

一切都工作,直到该路由

包括 update_resource(资源,则params)方法给控制器,并在视图中删除 current_password字段,所建议 https://github.com/plataformatec/devise/wiki/How-To:-Allow-users-to-edit-their-account-without-providing-a-password

更改后,我可以编辑除密码之外的其他所有字段(first_name,last_name ... etc)。密码更改不会持续。任何想法?

的Rails版本:5.0.0.1 设计版本:4.2.0

回答

0

account_update_params方法,你需要添加passwordpassword_confirmation

def account_update_params 
    devise_parameter_sanitizer.permit(:account_update, keys: [:first_name, :last_name, :cpf, :birth_date, :phone, :gender, :password, :password_confirmation]) 
end 
+0

我试过Charn并没有工作。 =/ – user1301037

+0

更新密码时在开发日志中出现任何错误 –

+0

不,在编辑视图中编辑密码和password_confirmation字段后,我通过成功消息重定向到根目录,但新密码未通过。 日志显示 - 完成401未授权259ms(ActiveRecord:1.1ms) – user1301037