如何将Ruby on Rails JSONAPI资源中的命名空间模型序列化?

2026-04-11 14:551阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计339个文字,预计阅读时间需要2分钟。

如何将Ruby on Rails JSONAPI资源中的命名空间模型序列化?

我在尝试在Rails引擎中使用JSONAPI::Resources,我在doki_core/app/resources/tenant.rb中定义了DokiCore::Tenant模型,在doki_core/app/resources/tenant_resource.rb中定义了DokiCore::TenantResource。

我正在尝试在Rails引擎中使用 JSONAPI Resources,我在doki_core / app / resources / tenant.rb中定义了DokiCore :: Tenant(模型),在doki_core / app / resources / tenant_resource.rb中定义了DokiCore :: TenantResource.当我尝试序列化为哈希时,我遇到以下错误:

NoMethodError: undefined method tenant_path' for #<Module:0x007f9d04208778>
from /Users/typeoneerror/.rvm/gems/ruby-2.2.2@doki/gems/jsonapi-resources-0.6.1/lib/jsonapi/link_builder.rb:77:in
public_send’

资源使用model_name让它知道模型的实际位置:

module DokiCore class TenantResource < JSONAPI::Resource model_name 'DokiCore::Tenant' # ... end end

我正在尝试输出像这样的租户的哈希:

tenant = DokiCore::Tenant.find(1); resource = DokiCore::TenantResource.new(tenant, nil); serializer = JSONAPI::ResourceSerializer.new(DokiCore::TenantResource); serializer.serialize_to_hash(resource);

这是错误发生的地方.

如何让链接正常工作和/或禁用它们?我假设它在那里它将资源的URL添加为输出的json中“links”键下的链接.

对此进行排序.如果您的路由以任何方式命名空间,则您的资源也需要命名空间才能匹配.我的路线看起来像:

如何将Ruby on Rails JSONAPI资源中的命名空间模型序列化?

namespace :api do namespace :v1 do resources :tenants end end

所以资源需要以相同的方式命名空间:

tenant = DokiCore::Tenant.find(1); resource = DokiCore::API::V1::TenantResource.new(tenant, nil); serializer = JSONAPI::ResourceSerializer.new(DokiCore::API::V1::TenantResource); serializer.serialize_to_hash(resource);

本文共计339个文字,预计阅读时间需要2分钟。

如何将Ruby on Rails JSONAPI资源中的命名空间模型序列化?

我在尝试在Rails引擎中使用JSONAPI::Resources,我在doki_core/app/resources/tenant.rb中定义了DokiCore::Tenant模型,在doki_core/app/resources/tenant_resource.rb中定义了DokiCore::TenantResource。

我正在尝试在Rails引擎中使用 JSONAPI Resources,我在doki_core / app / resources / tenant.rb中定义了DokiCore :: Tenant(模型),在doki_core / app / resources / tenant_resource.rb中定义了DokiCore :: TenantResource.当我尝试序列化为哈希时,我遇到以下错误:

NoMethodError: undefined method tenant_path' for #<Module:0x007f9d04208778>
from /Users/typeoneerror/.rvm/gems/ruby-2.2.2@doki/gems/jsonapi-resources-0.6.1/lib/jsonapi/link_builder.rb:77:in
public_send’

资源使用model_name让它知道模型的实际位置:

module DokiCore class TenantResource < JSONAPI::Resource model_name 'DokiCore::Tenant' # ... end end

我正在尝试输出像这样的租户的哈希:

tenant = DokiCore::Tenant.find(1); resource = DokiCore::TenantResource.new(tenant, nil); serializer = JSONAPI::ResourceSerializer.new(DokiCore::TenantResource); serializer.serialize_to_hash(resource);

这是错误发生的地方.

如何让链接正常工作和/或禁用它们?我假设它在那里它将资源的URL添加为输出的json中“links”键下的链接.

对此进行排序.如果您的路由以任何方式命名空间,则您的资源也需要命名空间才能匹配.我的路线看起来像:

如何将Ruby on Rails JSONAPI资源中的命名空间模型序列化?

namespace :api do namespace :v1 do resources :tenants end end

所以资源需要以相同的方式命名空间:

tenant = DokiCore::Tenant.find(1); resource = DokiCore::API::V1::TenantResource.new(tenant, nil); serializer = JSONAPI::ResourceSerializer.new(DokiCore::API::V1::TenantResource); serializer.serialize_to_hash(resource);