Ruby on rails Rails:NoMethodError-未定义的方法'record\u type`

Ruby on rails Rails:NoMethodError-未定义的方法'record\u type`,ruby-on-rails,serialization,json-api,Ruby On Rails,Serialization,Json Api,我正在使用 宝石 我对应用程序中的资源使用名称空间,对序列化程序文件也使用名称空间 因此,我的序列化程序文件如下所示: module Applyportal class ApplicantSerializer include JSONAPI::Serializer attributes :first_name, :middle_name, :last_name, :phone, :email, :username, :password, :nationality, :sta

我正在使用 宝石

我对应用程序中的资源使用名称空间,对序列化程序文件也使用名称空间

因此,我的序列化程序文件如下所示:

module Applyportal
  class ApplicantSerializer
    include JSONAPI::Serializer
    attributes :first_name, :middle_name, :last_name, :phone, :email, :username, :password,  :nationality, :state_of_origin, :local_government

    belongs_to :local_government, serializer: Personalinfo::LocalGovernment
    belongs_to :marital_status, serializer: Personalinfo::MaritalStatus
    belongs_to :nationality, serializer: Personalinfo::Nationality
    has_many :applications, serializer: Applyportal::Application
    cache_options store: Rails.cache, namespace: 'jsonapi-serializer', expires_in: 1.hour
  end
end
module Applyportal
  class ApplicantSerializer
    include JSONAPI::Serializer
    attributes :first_name, :middle_name, :last_name, :phone, :email, :username, :password,  :nationality, :state_of_origin, :local_government

    belongs_to :local_government, serializer: Personalinfo::LocalGovernment
    belongs_to :marital_status, serializer: Personalinfo::MaritalStatus
    belongs_to :nationality, serializer: Personalinfo::Nationality
    has_many :applications, serializer: Applyportal::Application
    cache_options store: Rails.cache, namespace: 'jsonapi-serializer', expires_in: 1.hour
  end
end
但是,当我尝试向该资源发出Post请求时,我得到以下错误:

NoMethodError (undefined method `record_type' for #<Class:0x00007f05d4d451f8>
Did you mean?  record_timestamps)
NoMethodError(未定义的“记录类型”方法)#
你是说?记录时间戳)

我似乎不知道问题是什么。

问题来自于我如何定义序列化程序关联

以下是我解决问题的方法

因此,我对应用程序中的资源使用了名称空间,对序列化程序文件也使用了名称空间

因此,我的序列化程序文件如下所示:

module Applyportal
  class ApplicantSerializer
    include JSONAPI::Serializer
    attributes :first_name, :middle_name, :last_name, :phone, :email, :username, :password,  :nationality, :state_of_origin, :local_government

    belongs_to :local_government, serializer: Personalinfo::LocalGovernment
    belongs_to :marital_status, serializer: Personalinfo::MaritalStatus
    belongs_to :nationality, serializer: Personalinfo::Nationality
    has_many :applications, serializer: Applyportal::Application
    cache_options store: Rails.cache, namespace: 'jsonapi-serializer', expires_in: 1.hour
  end
end
module Applyportal
  class ApplicantSerializer
    include JSONAPI::Serializer
    attributes :first_name, :middle_name, :last_name, :phone, :email, :username, :password,  :nationality, :state_of_origin, :local_government

    belongs_to :local_government, serializer: Personalinfo::LocalGovernment
    belongs_to :marital_status, serializer: Personalinfo::MaritalStatus
    belongs_to :nationality, serializer: Personalinfo::Nationality
    has_many :applications, serializer: Applyportal::Application
    cache_options store: Rails.cache, namespace: 'jsonapi-serializer', expires_in: 1.hour
  end
end
我没有在每个关联的末尾添加
序列化程序
,这使得rails应用程序很难找到
记录类型
,因此:

belongs_to :local_government, serializer: Personalinfo::LocalGovernment
应该是

belongs_to :local_government, serializer: Personalinfo::LocalGovernmentSerializer
因此,我的序列化程序文件后来看起来像这样:

module Applyportal
  class ApplicantSerializer
    include JSONAPI::Serializer
    attributes :first_name, :middle_name, :last_name, :phone, :email, :username, :password,  :nationality, :state_of_origin, :local_government

    belongs_to :local_government, serializer: Personalinfo::LocalGovernmentSerializer
    belongs_to :marital_status, serializer: Personalinfo::MaritalStatusSerializer
    belongs_to :nationality, serializer: Personalinfo::NationalitySerializer
    has_many :applications, serializer: Applyportal::ApplicationSerializer
    cache_options store: Rails.cache, namespace: 'jsonapi-serializer', expires_in: 1.hour
  end
end
就这些

我希望这有帮助