Rails设计无法更新用户模型字段

时间:2016-12-11 16:51:39

标签: ruby-on-rails-4 devise

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

日志:

  

在2016-12-11 18:34:51 +0200开始为127.0.0.1启用PATCH“/ users”   由RegistrationsController处理#update为HTML     参数:{“utf8”=>“✓”,“authenticity_token”=>“5vqLuQKyuJIoCx0mYfBUhIQmvS0Il5jy + ty1f2XDsehJM4i3M8mMPK5tdPpVQ6MFHIj05ZcRKO7rhlxroM75pQ ==”,“user”=> {“first_name”=>“Birhanu”,“last_name”=> “Hailemariam2”,“email”=>“birhanuh4@gmail.com”,“password”=>“[FILTERED]”,“password_confirmation”=>“[FILTERED]”,“current_password”=>“[已过滤]“},”提交“=>”更新用户“}     用户负载(0.7ms)SELECT“users”。* FROM“users”WHERE“users”。“id”= $ 1 ORDER BY“users”。“id”ASC LIMIT 1 [[“id”,1]]     帐户加载(0.4ms)SELECT“public”。“accounts”。* FROM“public”。“accounts”WHERE“public”。“accounts”。“subdomain”= $ 1 LIMIT 1 [[“subdomain”,“test3”] ]     用户负载(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   重定向到http://test3.lvh.me:3000/users/1   完成302发现在120ms(ActiveRecord:4.5ms)

在2016-12-11 18:34:51 +0200

开始获取127.0.0.1的“/ users / 1”

视图/设计/注册/ 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'

控制器/ 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

控制器/ 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' }

1 个答案:

答案 0 :(得分:0)

我设法找到导致问题的原因。这是因为我有一个私有方法将false值返回到before_save ActiveRecord回调。事实来源:enter link description here

以前的版本:

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

如果值为false,则应将其更改为nil

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

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