Rails devise无法更新用户模型字段 [英] Rails devise could't update User model fields

查看:72
本文介绍了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年度开始为127.0.0.1启动PATCH /用户 -11 18:34:51 +0200
由RegistrationsController#update处理为HTML
参数:{ utf8 =>✓, authenticity_token => 5vqLuQKyuJIoCx0mYfBUhIQmvS0Il5jy + ty1f2XDsehJMK4Q3M7J7K4K6M6用户 => {名字 => Birhanu,姓氏 => Hailemariam2,电子邮件 => birhanuh4@gmail.com,密码 => [已过滤],密码确认 => [FILTERED], current_password => [FILTERED]}, commit =>更新用户}
用户负载(0.7ms)选择 users。* FROM users WHERE users。 id = $ 1 ORDER BY users。 id ASC LIMIT 1 [[ id,1]]
帐户加载(0.4ms)选择 public。 accounts。 *从 public。 accounts到 public。 accounts。 subdomain = $ 1 LIMI T 1 [[ subdomain, test3]]
用户负载(0.4ms)选择 users。*从 users位置 users。 id = $ 1 LIMIT 1 [[ id ,1]]
(0.3ms)开始
(0.7ms)从用户中选择COUNT(*)
(0.3ms)ROLLBACK
重定向到 http://test3.lvh.me:3000/users/1
已完成302找到120ms(ActiveRecord:4.5ms)

Started PATCH "/users" for 127.0.0.1 at 2016-12-11 18:34:51 +0200 Processing by RegistrationsController#update as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"5vqLuQKyuJIoCx0mYfBUhIQmvS0Il5jy+ty1f2XDsehJM4i3M8mMPK5tdPpVQ6MFHIj05ZcRKO7rhlxroM75pQ==", "user"=>{"first_name"=>"Birhanu", "last_name"=>"Hailemariam2", "email"=>"birhanuh4@gmail.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "current_password"=>"[FILTERED]"}, "commit"=>"Update User"} User Load (0.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]] Account Load (0.4ms) SELECT "public"."accounts".* FROM "public"."accounts" WHERE "public"."accounts"."subdomain" = $1 LIMIT 1 [["subdomain", "test3"]] User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]] (0.3ms) BEGIN (0.7ms) SELECT COUNT(*) FROM "users" (0.3ms) ROLLBACK Redirected to http://test3.lvh.me:3000/users/1 Completed 302 Found in 120ms (ActiveRecord: 4.5ms)

在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无法更新用户模型字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆