我配置了json-resouce-api。 但是json-resource-api生成的自我链接是错误的。
代码似乎检查资源类的模块层次结构,并完全忽略rails如何生成路由。
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
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
我得到了什么=&gt; http://api.localhost.local:3000/api/v1/subscriptions/1
但它应该是http://api.localhost.local:3000/subscriptions/1
我该如何解决这个问题?
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
此问题与github.com/cerebris/jsonapi-resources/issues/591完全相同 猴子包可以应用,但它有点冒险。 现在(2016年10月5日),除了
之外,我找不到任何其他方式 namespace :api do
namespace :v1 do
jsonapi_resources :subscriptions
end
end
答案 0 :(得分:0)
你可以尝试一下:
namespace :api, defaults: { format: :json }, constraints: { subdomain: 'api' }, path: "" do
namespace :v1, path: "" do
jsonapi_resources :subscriptions
end
end