【发布时间】:2012-01-09 07:47:00
【问题描述】:
我正在开发 ROR 应用程序,但在设计和密码确认方面遇到了困难。我希望我的用户能够编辑他们的信息(姓名、位置等),而无需输入和确认他们的密码(除非他们决定更改密码,在这种情况下,这些字段将是必需的。 )
我在设计方面做了一些阅读,我注意到这是一个常见问题。不幸的是,我尝试了 devise GitHub repo 上发布的所有解决方案,但无法解决我的问题。
然而,我发现了一种解决方法,但我希望将其作为最后一步时遇到问题。这是我的players.rb 文件的小sn-p(我有两组帐户——玩家和所有者):
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| attributes['password'].blank? } 也会执行!这意味着如果玩家不输入密码,而不是提示他输入密码,应用程序就会刹车!
我需要一些帮助来编写 if 语句或某些条件,如果帐户属于已注册(现有)玩家,基本上只会触发 reject_if 条件。
我试过了::reject_if => proc { |attributes| attributes['password'].blank? unless Player.new}
和
if Player.new
accepts_nested_attributes_for :account
else
accepts_nested_attributes_for :account, :reject_if => proc { |attributes| attributes['password'].blank? }
end
我似乎无法弄清楚这一点,所以我决定看看是否有人可以提供意见或建议。与往常一样,我非常感谢您的时间和您可能提供的任何帮助。谢谢!
【问题讨论】:
标签: ruby ruby-on-rails-3 devise