我正在开发一个 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
我似乎无法弄清楚这一点,所以我决定看看是否有人可以提供意见或建议。一如既往,我非常感谢您的宝贵时间以及您提供的任何帮助。谢谢你!