Active Record:NameError(nil:NilClass 的未定义局部变量或方法“属性”,您是说吗?attribute_names)

Active Record: NameError (undefined local variable or method `attributes' for nil:NilClass Did you mean? attribute_names)

我最近从 Rails 6.1 迁移到 7.0,但我在尝试注册用户时一直收到错误消息。

NameError (undefined local variable or method `attributes' for nil:NilClass
Did you mean?  attribute_names):
  
activemodel (7.0.2.2) lib/active_model/serialization.rb:153:in `attribute_names'
actionpack (7.0.2.2) lib/action_controller/metal/params_wrapper.rb:118:in `block in include'
/Users/axel/.rvm/rubies/ruby-3.0.0/lib/ruby/3.0.0/mutex_m.rb:78:in `synchronize'
/Users/axel/.rvm/rubies/ruby-3.0.0/lib/ruby/3.0.0/mutex_m.rb:78:in `mu_synchronize'
actionpack (7.0.2.2) lib/action_controller/metal/params_wrapper.rb:112:in `include'
actionpack (7.0.2.2) lib/action_controller/metal/params_wrapper.rb:278:in `_extract_parameters'

有些事情告诉我我需要在用户模型中实现一些 attribute 可能是因为我对 email 进行了验证。我试过了

include ActiveModel::Serialization

  attr_accessor :email

  def attributes
    {'email' => nil}
  end

但没有任何效果。

这是我的用户模型

require 'druuid'

class User < ApplicationRecord

  before_create :downcase_email
  before_update :downcase_email

  # No confirmations or password resets in Development
  if Rails.env.production?
    devise :database_authenticatable, :registerable, :confirmable,
           :recoverable, :rememberable, :validatable, :lockable,
           :omniauthable, omniauth_providers: [:google_oauth2],
           authentication_keys: [:email]
  else
    devise :database_authenticatable, :registerable,
           :recoverable, :rememberable, :validatable,
           :omniauthable, omniauth_providers: [:google_oauth2],
           authentication_keys: [:email]
  end

  # ------------- validations -------------

  validates :email,
            presence: { message: "can't be empty"},
            :uniqueness =>  {:case_sensitive => false}

  def downcase_email
    self.email = self.email.downcase
  end

  # ------------- Omniauth -------------

  # Check Omniauth Controller
  def self.from_omniauth(auth)

    user = User.where(email: auth.info.email).first

    if user
      # record.update() returns a boolean value, user is automatically updated
      user.update(refresh_token: auth.credentials.refresh_token,
                  access_token: auth.credentials.token,
                  provider_uid: auth.uid,
                  provider: auth.provider,
                  )
    else
      user = User.create(email: auth.info.email,
                         provider_uid: auth.uid,
                         provider: auth.provider,
                         refresh_token: auth.credentials.refresh_token,
                         access_token: auth.credentials.token,
                         password: Devise.friendly_token[0, 20],
                         firstname: auth.info.first_name,
                         lastname: auth.info.last_name,
                         )
    end
    user
  end

end

这是我的带有 ActiveModelSerializers 的序列化器

class UserSerializer < ActiveModel::Serializer
              # Basic
  attributes :id, :email, :uid, :firstname, :lastname
end

您需要在初始化序列化程序的任何地方包含活动模型序列化。

例如,在您的 /application_controller.rb:

class ApplicationController < ActionController::API
    include ActiveModel::Serialization
end