【发布时间】:2017-10-16 22:19:48
【问题描述】:
我正在使用这个gem,但在实现多态关系时遇到了一些麻烦。
我有一个 polymorphic 和 products 资源。我也有适当的控制器和路线。我也有常规的 ActiveRecord 模型:
我的模型:
class Product < ActiveRecord::Base
include Priceable
belongs_to :producible, polymorphic: true
...
我有这样的担忧,我将其包含在producible 类中,例如Beer 或Apples
module Producible
extend ActiveSupport::Concern
included do
has_one :product, as: :producible, inverse_of: :producible
我的路线:
jsonapi_resource :products
和
/api/products/relationships/producible
我的资源(我听说我也需要多态资源)。
class Api::ProductResource < JSONAPI::Resource
attributes :producible_type, :plan_id, :is_selling_enabled
belongs_to :producible, polymorphic: true
end
class Api::ProducibleResource < JSONAPI::Resource
end
我的控制器:
module Api
class ProductsController < Api::BaseJsonapiController
end
end
和
module Api
class ProduciblesController < Api::BaseJsonapiController
end
end
当我尝试导航到顶部列出的路线时出现此错误:
{
"errors": [
{
"title": "Missing Parameter",
"detail": "The required parameter, product_id, is missing.",
"code": "106",
"status": "400"
}
]
}
这很奇怪,因为路线似乎不需要 product_id 对吧?
【问题讨论】:
-
您的可生产模型中有
product_id吗? -
所以错误是我使用的路线。我认为使用
jsonapi_resource :products而不是jsonapi_resources :products会因为某种原因导致失败。 -
我的
products表中有一个producible_id。