允许用户编辑帐户而不保存密码传递条件到:reject_if在Ruby中 [英] Allowing users to edit accounts without saving passwords in devise & passing conditions to :reject_if in Ruby

查看:98
本文介绍了允许用户编辑帐户而不保存密码传递条件到:reject_if在Ruby中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ROR应用程序,当涉及到Devise和密码确认时,我被困住了。我希望我的用户能够编辑他们的信息(名称,位置等),而无需输入和确认他们的密码(除非他们决定更改密码,否则这些字段是必需的。 )

I am working on a ROR app, and I'm stuck when it comes to Devise and password confirmation. I'd like for my users to be able to edit their information (name, location, etc) without having to enter and confirm their password (that is unless they decide to change their password, in which case, these fields would be required.)

我在设计时做了一些阅读,我注意到这是一个常见的问题。不幸的是,我尝试了所有的解决方案发布在设计GitHub repo,但无法解决我的问题。

I did a bit of reading when it comes to devise and I noticed that this is a common issue. Unfortunately, I tried all of the solutions posted on the devise GitHub repo, but was unable to address my issue.

然而,我发现了一个解决方法,但有一个我希望这将是最后一步的问题。这是一个小代码片段,我的 players.rb 文件看起来像(我有两套账户 - 玩家和所有者):

I have however, discovered a workaround, but am having an issue with what I hope will be the final step. Here is a small snippet of what my players.rb file looks like (I have two set's of accounts -- players and owners):

 has_one :account, :as => :profile

 accepts_nested_attributes_for :account, :reject_if => proc { |attributes| attributes['password'].blank? }

我的 accounts.rb 这是:

  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me, :invited_by, :invited_by_id

由于正在设置,播放器可以编辑他们的个人资料,而无需输入密码,除非他们尝试编辑:密码字段 - 这是非常好的,我想要的结果。但是,我正在陷入一个小小的障碍... :reject_if => proc {| attributes |属性[密码。空白?即使有一个新的帐号(播放器)被创建,也可以执行!这意味着如果玩家没有输入密码,而不是提示他输入密码,应用程序会刹车!

As it's set up right now, the players can edit their profile without having to enter a password unless they are trying to edit the :password field -- this is excellent, and my desired result. However, I am running into a small snag... the :reject_if => proc { |attributes| attributes['password'].blank? } executes even when there is a new account (player) being created! This means if a player does not enter a password, instead of prompting him to enter a password, the application brakes!

我需要一些帮助,写一个if语句或一些条件如果帐户属于已注册的(现有的)玩家,那么基本上只会触发 reject_if 条件。

I need some help writing an if statement or some condition that would basically only trigger the reject_if conditions if the Account belongs to a registered (existing) player.

我有尝试::reject_if => proc {| attributes |属性[密码。空白?除非Player.new}

if Player.new
accepts_nested_attributes_for :account
else
accepts_nested_attributes_for :account, :reject_if => proc { |attributes| attributes['password'].blank? }
end

我似乎没有想到这一点,所以我决定看看是否有人可以提出意见或建议。和往常一样,我非常感谢您的时间和您可能提供的任何帮助。谢谢!

I cant seem to figure this out, so I decided to see if anyone could offer an opinion or advice. As always, I am extremely grateful for your time and any help you may offer. Thank you!

推荐答案

在更新控制器中的用户时,尝试安全的代码。这是devise版本1.5.3的更新方法,这两行会做的伎俩。

Try this peace of code while updating user in controller. This is update method for devise version 1.5.3, these two lines will do the trick.

 def update
    self.resource = resource_class.to_adapter.get!(send(:"current_#{resource_name}").to_key)

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

    if resource.update_attributes(params[resource_name]) 
      set_flash_message :notice, :updated if is_navigational_format?
      sign_in resource_name, resource, :bypass => true
      respond_with resource, :location => after_update_path_for(resource)
    else
      clean_up_passwords(resource)
      respond_with_navigational(resource){ render_with_scope :edit }
    end
  end

这篇关于允许用户编辑帐户而不保存密码传递条件到:reject_if在Ruby中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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