0

我关注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 动作中的任何一个。

4

3 回答 3

2

该路线应类似于以下代码片段:

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

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

于 2015-07-05T17:19:35.300 回答
2

考虑到我的担忧,因为您可能必须在几个资源中使用它(rails doc 写得很好

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
于 2015-07-05T17:29:24.390 回答
0

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

我得到了它的工作:

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

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

于 2015-12-26T22:15:34.970 回答