gpt4 book ai didi

ruby - 允许用户在不保存密码的情况下编辑帐户并将条件传递给 :reject_if in Ruby

转载 作者:太空宇宙 更新时间:2023-11-03 18:27:40 25 4
gpt4 key购买 nike

我正在开发一个 ROR 应用程序,但在设计和密码确认方面我遇到了困难。我希望我的用户能够编辑他们的信息(姓名、位置等)而无需输入和确认他们的密码(除非他们决定更改密码,在这种情况下,这些字段将是必需的。 )

关于设计,我做了一些阅读,我注意到这是一个常见问题。不幸的是,我尝试了 devise GitHub 存储库上发布的所有解决方案,但无法解决我的问题。

不过,我发现了一个解决方法,但我对最后一步有疑问。这是我的 players.rb 文件的一小段内容(我有两组帐户——玩家和所有者):

 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

按照现在的设置,玩家无需输入密码即可编辑他们的个人资料,除非他们试图编辑 :password 字段——这非常好,也是我想要的结果。但是,我遇到了一个小问题...... :reject_if => proc { |attributes|属性['密码'].空白? 即使在创建新帐户(玩家)时也会执行!这意味着如果玩家不输入密码,应用程序不会提示他输入密码,而是会停止!

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

我试过::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

我似乎无法弄清楚这一点,所以我决定看看是否有人可以提供意见或建议。一如既往,我非常感谢您抽出宝贵时间以及您可能提供的任何帮助。谢谢!

最佳答案

这个问题困扰了我很长时间。按照“操作方法”解决它,然后将 :current_password 添加到 attr_accessible 并使用以下内容创建一个 attr_accessor:

attr_accessor :password, :password_confirmation, :current_password
attr_accessible :email, :password, :password_confirmation, :remember_me, :username, :current_password

如果其他人遇到这个问题,这里是帮助我解决问题的相关链接:

1) How To: Allow users to edit their account without providing a password

2) Can't resolve "Current password can't be blank" when editing user

3) Problems trying' to update the user profile info without password (this is the one that solved it for me)

另外,这里有一个关于如何通过 Devise & Omniauth 对 Facebook 做同样的事情的线索:

4) Editing Users With Devise and Omniauth

关于ruby - 允许用户在不保存密码的情况下编辑帐户并将条件传递给 :reject_if in Ruby,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8785290/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com