允许用户编辑帐户,而无需在设计中保存密码并将条件传递给 Ruby 中的 :reject_if

2024-04-19

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

我在设计方面读了一些书,发现这是一个常见问题。不幸的是,我尝试了 devise GitHub 存储库上发布的所有解决方案,但无法解决我的问题。

然而,我发现了一种解决方法,但对我希望的最后一步有疑问。这是我的一小段内容players.rb文件看起来像(我有两组帐户 - 玩家和所有者):

 has_one :account, :as => :profile

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

my 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| attributes['password'].blank? }即使创建了新帐户(玩家)也会执行!这意味着如果玩家不输入密码,应用程序将停止运行,而不是提示他输入密码!

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

我努力了::reject_if => proc { |attributes| attributes['password'].blank? unless Player.new}

and

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) 操作方法:允许用户在不提供密码的情况下编辑其帐户 https://github.com/plataformatec/devise/wiki/How-To:-Allow-users-to-edit-their-account-without-providing-a-password

2) 编辑用户时无法解决“当前密码不能为空”的问题 https://groups.google.com/forum/#!topic/plataformatec-devise/1eV4WzH6myc

3)尝试在没有密码的情况下更新用户个人资料信息时出现问题(这是为我解决的问题) https://github.com/plataformatec/devise/issues/1620

此外,这里还提供了如何通过 Devise 和 Omniauth 对 Facebook 进行相同操作的指南:

4) 使用 Devise 和 Omniauth 编辑用户 https://stackoverflow.com/questions/13436232/editing-users-with-devise-and-omniauth#comment18368874_13436232

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

允许用户编辑帐户,而无需在设计中保存密码并将条件传递给 Ruby 中的 :reject_if 的相关文章

随机推荐