Rails: NoMethodError - 未定义的方法`record_type`
Rails: NoMethodError - undefined method `record_type`
我正在开发 Rails 6 API 仅应用程序使用
jsonapi-serializer gem.
我在我的应用程序中为资源使用了命名空间,我也为序列化程序文件使用了命名空间。
所以我的序列化程序文件如下所示:
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)
我似乎无法弄清楚问题是什么。
问题出在我定义序列化程序关联的方式上
我是这样解决的:
所以我在我的应用程序中为资源使用了命名空间,我也为序列化程序文件使用了命名空间。
所以我的序列化程序文件看起来像这样:
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
我没有在每个关联的末尾添加 Serializer
,这使得 rails 应用程序很难找到 record_type
,所以不是:
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
就这些了。
希望对您有所帮助
我正在开发 Rails 6 API 仅应用程序使用 jsonapi-serializer gem.
我在我的应用程序中为资源使用了命名空间,我也为序列化程序文件使用了命名空间。
所以我的序列化程序文件如下所示:
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)
我似乎无法弄清楚问题是什么。
问题出在我定义序列化程序关联的方式上
我是这样解决的:
所以我在我的应用程序中为资源使用了命名空间,我也为序列化程序文件使用了命名空间。
所以我的序列化程序文件看起来像这样:
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
我没有在每个关联的末尾添加 Serializer
,这使得 rails 应用程序很难找到 record_type
,所以不是:
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
就这些了。
希望对您有所帮助