devise无法更新用户模型字段

devise无法更新用户模型字段

本文介绍了Rails devise无法更新用户模型字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法按照设备本身页面上建议的步骤来更新用户信息。当我单击表单中的更新按钮时,它将重定向回用户/显示而不更新任何内容。控制台上的日志显示调用了 RegistrationsController#update 操作,而不是我的自定义RegistrationController中的 update_resource 操作。

I couldn't update user information following suggested procedures on devise's own page. When I click the Update button in the form it redirects back to to user/show without updating anything. The logs on console shows the RegistrationsController#update action was called instead the update_resource action from my custom RegistrationController.

日志:

在2016-12-11 18开始为127.0.0.1开始获取 / users / 1: 34:51 +0200

Started GET "/users/1" for 127.0.0.1 at 2016-12-11 18:34:51 +0200

视图/设计/注册/edit.slim

views/devise/registrations/edit.slim

= simple_form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f|
          = f.input :first_name, autofocus: true
          = f.input :last_name, autofocus: true
          = f.input :email, autofocus: true

          - if devise_mapping.confirmable? && resource.pending_reconfirmation?
            div
              |Currently waiting confirmation for: = resource.unconfirmed_email

          .form-actions.col-sm-offset-3.m-b-xs
            i
              |(leave blank if you don't want to change it)
          = f.input :password, autocomplete: "off"

          = f.input :password_confirmation, autocomplete: "off"

          .form-actions.col-sm-offset-3.m-b-xs
            i
              |(we need your current password to confirm your changes)
          = f.input :current_password, autocomplete: "off"

          .form-actions.form.col-sm-offset-3
            => link_to I18n.t('button.cancel'), :back, class: 'btn btn-primary btn-outline'
            = f.button :submit, class: 'btn btn-primary'

controller / registrations_controller.rb

controller/registrations_controller.rb

class RegistrationsController < Devise::RegistrationsController

  protected
  def update_resource(resource, user_params)
    resource.update_with_password(user_params)
  end

  def after_update_path_for(resource)
    current_account
  end

end

controllers / application_controller.rb

controllers/application_controller.rb

class ApplicationController < ActionController::Base
  .
  .
  before_filter :configure_permitted_parameters, if: :devise_controller?

  protected
  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:accept_invitation, keys: [:first_name, :last_name])
    devise_parameter_sanitizer.permit(:account_update, keys: [:first_name, :last_name, :email, :password, :password_confirmation,
        :current_password])
  end
  .
  .
end

路线:

devise_for :users, controllers: { registrations: 'registrations' }


推荐答案

我设法找到了引起问题的原因。这是因为我有一个私有方法,正在将 false 值返回到 before_save ActiveRecord回调。事实来源:

I have managed to find what was causing the issue. It is because I have a private method that was returning false value to before_save ActiveRecord callback. Fact source: enter link description here

先前版本:

private
  def set_admin
    self.admin = User.count == 0
  end

应更改为返回 nil 以防出现虚假值。

It should be changed to return nil in case of false value.

private
  def set_admin
    self.admin = User.count == 0
    nil
  end

现在一切正常,我可以更新用户信息!

Now everything works, I can update user info!

这篇关于Rails devise无法更新用户模型字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 19:01