Ruby on Rails,Devise宝石。密码空白时如何清除当前密码? [英] Ruby on Rails, Devise gem. How to remove current password when password is blank?

查看:106
本文介绍了Ruby on Rails,Devise宝石。密码空白时如何清除当前密码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我意识到这个话题已经被很多次覆盖了。但是,当数据库中的密码为空时,似乎没有想要使当前密码字段免除的解决方案。



我目前拥有必需的密码我的用户模型如下:

  def password_required? 
(authentications.empty?||!password.blank?)&&超级
结束

然后我将更新功能复制到我的注册控制器中:


  def update 
如果resource.update_with_password(params [resource_name])
set_flash_message:notice,,updated
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

我只是不知道如何确保设计在编辑空白密码时不需要密码,我还需要删除current_password字段从视图中执行?

 如果current_user.password_需要,则为% %GT; 
< p><%= f.label:current_password%> < i(我们需要您当前的密码来确认您的更改)< / i>< br />
<%= f.password_field:current_password%>< / p>
<%end%>
< p><%= f.submit更新%>< / p>

任何建议对我来说都是非常棒的,因为我相信我可以忽略某些东西,但我还是新手整个轨道谢谢!

解决方案

好的,所以我终于弄清楚了!



将以下内容添加到您的类RegistrationsController< devise :: RegistrationsController

  def update 
如果resource.update_with_password(params [resource_name])
set_flash_message:通知,更新
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

然后以下到您的用户模型:

  def update_with_password(params = {})
current_password = params.delete(:current_password)if!params [:current_password] .blank?

如果params [:password] .blank?
params.delete(:password)
params.delete(:password_confirmation)if params [:password_confirmation] .blank?
end

result = if has_no_password? || valid_password?(current_password)
update_attributes(params)
else
self.errors.add(:current_password,current_password.blank??:blank::invalid)
self.attributes = params
false
end

clean_up_passwords
result
end

def has_no_password?
self.encrypted_pa​​ssword.blank?
end

我稍微困惑的唯一的一点是在编辑视图中: / p>

 <%if!current_user.has_no_password? %GT; 

我把它包好了,如果,我会以为会是:

 <%if current_user.has_no_password? %GT; 

如果任何人可以看到任何东西来改善我的代码,或者使用不同的,更有效的方式让我知道! / p>

Now I realise this topic has been covered many times before. However there did not appear to be a solution that wanted to make the current password field exempt only when the password in the database was blank.

I currently have the password required in my user model like:

def password_required?
  (authentications.empty? || !password.blank?) && super
end

Then I copied the update function across into my registrations controller:

def update
  if resource.update_with_password(params[resource_name])
    set_flash_message :notice, :updated
    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

I just don't know how to go about ensuring that devise does not need a password when editing a blank password, do I also need to remove the current_password field from the view and do this?

<% if current_user.password_required? %>
  <p><%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br />
  <%= f.password_field :current_password %></p>
<% end %>
<p><%= f.submit "Update" %></p>

Any suggestions would be great on this as I am sure I am overlooking something but I am still new to Rails on a whole. Thanks!

解决方案

Okay so I have finally figured this out!

Add the following to your class RegistrationsController < Devise::RegistrationsController

def update
  if resource.update_with_password(params[resource_name])
    set_flash_message :notice, :updated
    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

Then the following to your User model:

def update_with_password(params={})
  current_password = params.delete(:current_password) if !params[:current_password].blank?

  if params[:password].blank?
    params.delete(:password)
    params.delete(:password_confirmation) if params[:password_confirmation].blank?
  end

  result = if has_no_password?  || valid_password?(current_password)
    update_attributes(params) 
  else
    self.errors.add(:current_password, current_password.blank? ? :blank : :invalid)
    self.attributes = params
    false
  end

  clean_up_passwords
  result
end

def has_no_password?
  self.encrypted_password.blank?
end

The only thing I was slightly confused about is that in the edit view:

<% if !current_user.has_no_password? %>

I wrapped it in that if, I would have thought it would have been:

<% if current_user.has_no_password? %>

If anyone can see anything to improve my code or a different, more efficient way let me know!

这篇关于Ruby on Rails,Devise宝石。密码空白时如何清除当前密码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆