【问题标题】:Allowing users to edit accounts without saving passwords in devise & passing conditions to :reject_if in Ruby允许用户在不保存密码的情况下编辑帐户并将条件传递给 Ruby 中的 :reject_if
【发布时间】: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


    【解决方案1】:

    这个问题困扰了我很长时间。按照“如何做”解决了它,然后将 :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

    【讨论】:

      【解决方案2】:

      在控制器中更新用户时尝试这种代码和平。这是设计版本 1.5.3 的更新方法,这两行就可以了。

       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
      

      【讨论】:

      • 对不起,这不起作用。这是 devise 通过 GitHub 提供的相同代码......我已经尝试过了,不幸的是,它没有成功。
      • 我收到以下错误:“未定义的局部变量或方法‘resource_class’”。我错过了什么吗?
      • 我猜你正在使用一些旧版本的设计,尝试安装最新版本的设计。表示删除 Gemfile.lock 然后捆绑安装。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-03
      • 2022-12-12
      • 1970-01-01
      • 2013-03-29
      • 1970-01-01
      • 1970-01-01
      • 2014-09-26
      相关资源
      最近更新 更多