为什么我的 Registrations#Update 设计操作没有更新我的“用户”模型中的所有属性?

Why does my Registrations#Update devise action not update all attributes in my `User` model?

这是我的 views/devise/registrations/edit.html.erb:

<%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put, class: "edit-user-form m-t" }) do |f| %>
      <%= f.error_notification %>    
        <div class="form-group">
          <%= f.input_field :email, required: true, autofocus: true %>
        </div>

        <div class="form-group">
          <%= f.input_field :current_password, hint: "we need your current password to confirm your changes", placeholder: "current password", required: true %>
        </div>

        <div class="form-group">
          <%= f.input_field :password, autocomplete: "off", hint: "leave it blank if you don't want to change it", placeholder: "new password", required: false %>
        </div>
        <div class="form-group">
          <%= f.input_field :password_confirmation, placeholder: "confirm new password", required: false %>
        </div>

        <div class="form-group">
            <%= f.association :school, collection:  School.where(school_type: [:college, :university]), prompt: "Choose a school", class: 'col-lg-4 form-control', label: false %>
        </div>
      </div>

      <div class="form-group">
          <div class="col-lg-12 text-center">
            <%= f.button :submit, "Update", class: "btn btn-lg edit-account-update-button" %>
          </div>
      </div>

    <% end %>

这是我的 ApplicationController:

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  before_action :configure_permitted_parameters, if: :devise_controller?
  check_authorization :unless => :devise_controller?

  rescue_from CanCan::AccessDenied do |exception|
    respond_to do |format|
      format.json { head :forbidden }
      format.html { redirect_back(fallback_location: root_path, flash: { danger: exception.message }) }
    end
  end

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:account_update, keys: [:avatar, :avatar_cache, :remove_avatar, :school_id])
  end
end

问题是它没有更新记录,这是该操作的日志:

Started PUT "/users" for ::1 at 2016-11-02 19:22:59 -0500
Processing by DeviseInvitable::RegistrationsController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"Xo445XVpElgDmfcjywZKEbsXIqZR/2Wgw==", "user"=>{"remove_avatar"=>"0", "avatar_cache"=>"", "email"=>"coach@test.com", "current_password"=>"[FILTERED]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "school_id"=>"3"}, "commit"=>"Update"}
  User Load (4.3ms)  SELECT  "users".* FROM "users" WHERE "users"."id" =  ORDER BY "users"."id" ASC LIMIT   [["id", 7], ["LIMIT", 1]]
  User Load (9.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" =  LIMIT   [["id", 7], ["LIMIT", 1]]
   (0.7ms)  BEGIN
  SQL (4.2ms)  UPDATE "users" SET "updated_at" = , "avatar" =  WHERE "users"."id" =   [["updated_at", 2016-11-03 00:22:59 UTC], ["avatar", "a1.jpg"], ["id", 7]]
   (0.8ms)  COMMIT
Redirected to http://localhost:3000/
Completed 302 Found in 139ms (ActiveRecord: 19.1ms)

请注意,school_id 是在参数中设置的,所以我们知道它正确地接受了来自表单的输入。但是 SQL UPDATE 语句不包含 school_id,因此它实际上并没有将该信息保存到数据库中。

另请注意,日志中没有 unpermitted params 条消息。

可能是什么原因造成的?

编辑 1

这是School.rb型号:

class School < ApplicationRecord
  has_many :users

  enum school_type: { high_school: 0, college: 1, university: 2 }
end

这是User.rb型号:

class User < ActiveRecord::Base
  belongs_to :school    
end

问题是您的设计模型 User 正在继承 ActiveRecord::Base 而不是 ApplicationRecord,它是 Rails 中的抽象 class 5. 继承后者,大多数处理模型的 gem 可以并且确实在它们自己的上下文中修改 ActiveRecord::Base 的代码而不影响其他模型,如果您继承前者,这是不可能的。

来源: