Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting the type of a resource #60

Closed
derekcannon opened this issue Apr 20, 2017 · 3 comments
Closed

Setting the type of a resource #60

derekcannon opened this issue Apr 20, 2017 · 3 comments

Comments

@derekcannon
Copy link

derekcannon commented Apr 20, 2017

Maybe I'm not understanding how jsonapi-utils/jsonapi-resource works, but I have a OrderResource and a OrderListResource (which is a simplified version of OrderResource). I am trying to render OrderListResource, but have the type value be order. Currently, it's setting it to order_list. Here's the render statement:

jsonapi_render json: Order.last(2), options: { resource: Api::V2::OrderListResource }

OrderListResource

module Api
  module V2
    class OrderListResource < JSONAPI::Resource
      attribute :status_name
    end
  end
end

Response

{
  "data": [
    {
      "id": "1",
      "type": "order_lists",  # <-- wrong, should be "order"
      "links": {
        "self": "http://localhost:3001/api/v2/order_lists/1"  # <-- wrong, should be order/1
      },
      "attributes": {
        "status_name": "some_status"
      }
    },
    {
      "id": "2",
      "type": "order_lists",   # <-- wrong, should be "order"
      "links": {
        "self": "http://localhost:3001/api/v2/order_lists/2" # <-- wrong, should be order/2
      },
      "attributes": {
        "status_name": "some_other_status"
      }
    }
  ]
}

EDIT:

I'm not sure if this is the proper solution, or if I'm using jsonapi-resource incorrectly, but I fixed it by adding the following to OrderListResource:

      def self.name
        'Order'
      end

      def self._type
        'order'
      end
@tiagopog
Copy link
Owner

tiagopog commented Apr 20, 2017

☝️ That's the answer I would bring up to you, as you mentioned it fixes the issue but I'm also not sure whether it's the proper way of doing so. Unfortunately JR's doc/issues are not clear around this case.

@Immaloserbigtime
Copy link

I sure hope this isn't what I think it is. Jeremy?
Jones need to speak with you.

@tiagopog
Copy link
Owner

tiagopog commented Jul 1, 2017

🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants