【问题标题】:Rails - How to override Devise 'current password' for omniauth usersRails - 如何覆盖为omniauth用户设计“当前密码”
【发布时间】:2017-06-01 13:38:23
【问题描述】:

我以前看过这个帖子,但没有找到适合我的解决方案。

我已尝试遵循 Devise Wiki 并尝试了其他人建议添加到控制器的代码: Editing Users With Devise and Omniauth [Rails]update_without_password does't update user info https://github.com/plataformatec/devise/issues/1620

问题是通过我们的 Twitter Omniauth 注册的用户无法更新他们的帐户信息,因为 Devise 会提示他们输入“当前密码”以进行更改。无法通过电子邮件向用户发送他们随机生成的密码。

我想做什么——

我想覆盖 Devise 控制器的“更新”方法,以便它识别第一次编辑其详细信息的 Twitter 用户并允许他们跳过“当前密码”验证。我们为 twitter 用户分配一个标准的电子邮件地址,它可以作为它检查的变量。他们将无法在不更改的情况下提交来自。

问题依旧——

我已经在注册控制器中尝试了不同的代码块,但我仍然被重定向回 from,并显示当前密码不正确。我什至没有进入 binding.pry ,所以看起来该方法甚至没有被读取?

我已尝试完全删除“当前密码”的表单输入,并使用默认值将其隐藏。

感谢您的帮助!

路线

Rails.application.routes.draw do

  ActiveAdmin.routes(self)
  # devise_for :users,
  devise_for :users, controllers: { registrations: 'registrations' },
  controllers: { omniauth_callbacks: 'users/omniauth_callbacks' }


  scope '(:locale)', locale: /en|ca|es/ do

    resources :politicians

    resources :posts do
      resources :comments, only:[:new, :create, :edit, :update, :destroy]
      member do
        put "like", to: "posts#upvote"
        put "dislike", to: "posts#downvote"
      end
    end

    get "/about" => "pages#about_us"
    root to: 'pages#home'
  end
  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end

Registrations_Controller.rb

class Users::RegistrationsController < Devise::RegistrationsController

  protected

   def update_resource(resource, params)
    binding.pry
      if current_user.email.include?("email.com")
        params.delete("current_password")
        resource.update_without_password(params)
      else
        resource.update_with_password(params)
      end
    end
end

【问题讨论】:

标签: ruby-on-rails ruby devise


【解决方案1】:

您需要在您的注册控制器中创建一个与此类似的方法:

def update_resource(resource, params)
    resource.update_without_password(params)
end

Devise 提供了一篇非常好的、有深度的 wiki 文章,允许用户在不输入密码 here 的情况下更新他们的帐户信息。

【讨论】:

  • 您好 thisischuck,感谢您的回复。我已经按照那里的步骤进行操作,但不幸的是,我被引导回表单并显示“我们需要您的当前密码来确认您的更改”错误消息。不知道我错过了什么
  • 确保您重新启动服务器并设置您的参数,以便它不会寻找密码确认字段。
猜你喜欢
  • 1970-01-01
  • 2017-06-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多