1

したがって、Facebook 経由で認証された場合にユーザーを編集するためのデバイス パスワード要件を回避することについて、およそ 50 万件の質問があることを私は知っています。私はそれらの少なくとも 75% を読んだことを約束しますが、まだこれを理解することはできません.

基本的に、Carl Edward & Laurie Laine の SO answer here に従って、Devise の登録コントローラーを作成しました。これにより、ユーザーがアカウントを編集していて、ユーザーが Facebook からログインしている場合にパスワードの検証をバイパスできます。次のコードでは、最終的にエラーはスローされませんが、更新された属性は保存されません。

class RegistrationsController < Devise::RegistrationsController

  def update_resource(resource, params)
    if current_user.provider == "facebook"
      params.delete("current_password")
      resource.update_without_password(params)
    else
      resource.update_with_password(params)
    end
  end

  def update
    account_update_params = devise_parameter_sanitizer.sanitize(:account_update)


    # required for settings form to submit when password is left blank
    if account_update_params[:password].blank?
      account_update_params.delete("password")
      account_update_params.delete("password_confirmation")
    end

    @user = User.find(current_user.id)
    if @user.update_attributes(account_update_params)
      @user.update(account_update_params)
      set_flash_message :notice, :updated
      update_resource(@user,account_update_params)
      # Sign in the user bypassing validation in case their password changed
      sign_in @user, :bypass => true
      redirect_to after_update_path_for(@user)
    else
      render "edit"
    end
  end
end

私は文字通り自分が間違っていることを理解できませんが、Facebookによって認証されたログインユーザーとしてユーザープロファイルを更新しようとするたびに、プロファイルやコンソールでデータベースにクエリを実行しても何も変わりません.

4

1 に答える 1

1

調べてみると、必要な属性が Devise パラメーター サニタイザーに含まれていたにもかかわらず、:account_update パラメーターの名前を :update と間違えていたことが判明しました。

前 (動作しない):

def configure_permitted_parameters
     devise_parameter_sanitizer.for(:sign_up) { |u| u.permit({ roles: [] }, :name,:email, :password, :password_confirmation) }
      devise_parameter_sanitizer.for(:update) { |u| u.permit({ roles: [] }, :email, :password, :password_confirmation, :avatar,:current_password, :about,:user, :name) }
  end
end

後(作業中):

def configure_permitted_parameters
     devise_parameter_sanitizer.for(:sign_up) { |u| u.permit({ roles: [] }, :name,:email, :password, :password_confirmation) }
      devise_parameter_sanitizer.for(:account_update) { |u| u.permit({ roles: [] }, :email, :password, :password_confirmation, :avatar,:current_password, :about,:user, :name) }
  end
end
于 2015-04-03T05:49:15.520 に答える