如何在Rails中构造如下JSONAPIurl?

How to construct the following JSONAPI url in Rails?

我正在关注 http://jsonapi.org/format/#document-top-level

我注意到以下终点(路线):

/articles/1/relationships/author

在Rails中,这条路线在routes.rb中如何构建?

resources :articles do
  # What goes here?
  # Should relationship be a namespace or other?
  # I guess author can be defined as a collection, or just a simple get
end

relationships 不需要执行 7 个 RESTFUL 操作中的任何一个。

路线应该像下面的代码片段:

resources :articles do
   resources :relationships, :only => [] do
     collection do 
       get :author
     end
   end
end

路由文件应该是这样的。如果需要任何更新,请告诉我。

跳出我的担忧想法,因为您可能不得不在多个资源中使用它(rails doc is quite well-written

concern :has_author do
  scope '/relationships' do
    resource :author
  end
end

resources :articles, concerns :has_author

相当于

resources :articles do
  scope :'/relationships' do
    resource :author
  end
end

scope :'/relationships' 对我不起作用(至少在 Rails 5)。

我用这个工作了:

resources :articles do
  resources :author, path: 'relationships/author'
end

这将只使用 AuthorController 并将其映射到 /articles/:id/relationships/author