Ruby on rails Rails 3.2 Omniauth设备-添加密码-跳过当前密码
我使用Omniauth允许用户通过Facebook、Twitter和谷歌登录(并创建帐户) 但是,如果用户决定不再使用这些服务,而是继续使用其帐户,他们将需要添加密码Ruby on rails Rails 3.2 Omniauth设备-添加密码-跳过当前密码,ruby-on-rails,authentication,devise,omniauth,Ruby On Rails,Authentication,Devise,Omniauth,我使用Omniauth允许用户通过Facebook、Twitter和谷歌登录(并创建帐户) 但是,如果用户决定不再使用这些服务,而是继续使用其帐户,他们将需要添加密码 如何允许用户在不输入当前密码的情况下向其帐户添加密码?* 当我让用户进入编辑用户注册路径(@user)时,他们会看到以下表格: = form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put,
如何允许用户在不输入当前密码的情况下向其帐户添加密码?*
当我让用户进入
编辑用户注册路径(@user)
时,他们会看到以下表格:
= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put, name: 'validation'}) do |f|
= devise_error_messages!
= f.label :email, class: 'block'
= f.email_field :email, class: 'large'
%span.infobar Keep this here unless you'd like to change your email
= f.label :password, class: 'block'
= f.password_field :password, class: 'large'
%span.infobar Leave blank if you don't want to change it
= f.label :password_confirmation, class: 'block'
= f.password_field :password_confirmation, class: 'large'
- if @user.password_required?
= f.label :current_password, class: 'block'
= f.password_field :current_password, class: 'large'
%span.infobar We need your current password to confirm changes
= f.submit "Update"
user.rb
def password_required?
(authentications.empty? || !password.blank?) && super
end
如您所见,我有它,因此如果需要
@user.password\u?
,则显示当前密码字段。即使在尝试为帐户创建新密码时不显示此字段,表单也不会正确验证,因为需要当前密码 我刚刚重写了RegistrationController#更新方法:
class RegistrationsController < Devise::RegistrationsController
def update
# Override Devise to use update_attributes instead of update_with_password.
# This is the only change we make.
if resource.update_attributes(params[resource_name])
set_flash_message :notice, :updated
# Line below required if using Devise >= 1.2.0
sign_in resource_name, resource, :bypass => true
redirect_to after_update_path_for(resource)
else
clean_up_passwords(resource)
render_with_scope :edit
end
end
end
类注册控制器=1.2.0,则需要下面的行
在资源中签名\u名称,资源::旁路=>true
将(资源)的路径重定向到更新后路径
其他的
清除密码(资源)
使用\u范围渲染\u:编辑
结束
结束
结束
来源:这对我不起作用,因为:password和:password\u确认属性已提交。如果密码的长度小于1,我会事先从参数中删除它们。