为什么我的注册#Update设计操作不会更新我的`User`模型中的所有属性?

时间:2016-11-03 00:40:08

标签: ruby-on-rails devise ruby-on-rails-5

这是我的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" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 7], ["LIMIT", 1]]
  User Load (9.1ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 LIMIT $2  [["id", 7], ["LIMIT", 1]]
   (0.7ms)  BEGIN
  SQL (4.2ms)  UPDATE "users" SET "updated_at" = $1, "avatar" = $2 WHERE "users"."id" = $3  [["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

1 个答案:

答案 0 :(得分:1)

问题是你的设计模型User继承了ActiveRecord::Base而不是ApplicationRecord这是Rails 5中的一个抽象类。随着后者的继承,大多数处理模型的宝石可以和在不影响其他模型的情况下,在自己的上下文中修改ActiveRecord::Base的代码,如果继承前者,这是不可能的。

来源:Why Rails 5 uses ApplicationRecord instead of ActiveRecord::Base?