1

I configured json-resouce-api. But the self link generated by json-resource-api is wrong.

The code seems to checks the module hierarchy of the resource class and completely ignores how rails generates the routes.

routes.rb

require 'api_constraints'

Rails.application.routes.draw do
  namespace :api, defaults: { format: :json }, constraints: { subdomain: 'api' }, path: '/'  do
    scope module: :v1, constraints: ApiConstraints.new(version: 1, default: true) do
      # resources :subscriptions, only: [:index, :new, :create]
      # jsonapi_resources :subscriptions, only: [:index, :new, :create]
      jsonapi_resources :subscriptions
    end
  end

resouces/api/V1/subscription_recource.rb

class Api::V1::SubscriptionResource < JSONAPI::Resource
   attributes :id, :third-service_id, :created_at, :updated_at
   model_name 'Subscription'

   # def custom_links(options)
   #  {self: nil}
   # end
end

What I got => http://api.localhost.local:3000/api/v1/subscriptions/1 but it should be http://api.localhost.local:3000/subscriptions/1

How can I fix this?

UPDATE

rake routes

[DUPLICATE ATTRIBUTE] `id` has already been defined in SubscriptionResource.
              Prefix Verb   URI Pattern                  Controller#Action
api_v1_subscriptions GET    /subscriptions(.:format)     api/v1/subscriptions#index {:format=>:json, :subdomain=>"api"}
                     POST   /subscriptions(.:format)     api/v1/subscriptions#create {:format=>:json, :subdomain=>"api"}
 api_v1_subscription GET    /subscriptions/:id(.:format) api/v1/subscriptions#show {:format=>:json, :subdomain=>"api"}
                     PATCH  /subscriptions/:id(.:format) api/v1/subscriptions#update {:format=>:json, :subdomain=>"api"}
                     PUT    /subscriptions/:id(.:format) api/v1/subscriptions#update {:format=>:json, :subdomain=>"api"}
                     DELETE /subscriptions/:id(.:format) api/v1/subscriptions#destroy {:format=>:json, :subdomain=>"api"}
        stripe_event        /stripe-events               StripeEvent::Engine

UPDATE2

This issue is totally the same as github.com/cerebris/jsonapi-resources/issues/591 Monkey Pack can be applied, but it's little bit risky. For now ( 2016, Oct 5th ), I couldn't find any other ways than

  namespace :api do
    namespace :v1 do
      jsonapi_resources :subscriptions
    end
  end
Community
  • 1
  • 1
Toshi
  • 6,012
  • 8
  • 35
  • 58

1 Answers1

0

You can try it:

namespace :api, defaults: { format: :json }, constraints: { subdomain: 'api' }, path: "" do
  namespace :v1, path: "" do
    jsonapi_resources :subscriptions
  end
end
Khanh Pham
  • 2,848
  • 2
  • 28
  • 36
  • Thanks for reply. But I'd love to remove `v1` from the uri. so that it should return `http://api.localhost.local:3000/subscriptions/1` – Toshi Oct 05 '16 at 07:17
  • Thanks again. but I still got "self": "http://api.localhost.local:3000/api/v1/subscriptions/1" – Toshi Oct 05 '16 at 07:29
  • @Toshi Can you run `rake routes | grep api` and show me your content ? – Khanh Pham Oct 05 '16 at 07:31
  • I think this issue is totally the same as https://github.com/cerebris/jsonapi-resources/issues/591 – Toshi Oct 05 '16 at 07:38