比如说,我有2个资源Author
和Book
,其中Author has_many Books
和Book belongs_to Author
。
这意味着通过执行以下操作:
# routes.rb
resources :books
resources :authors do
resources :books
end
我将有两条指向BooksController#index
的路线:
1)GET /books
和2)GET /authors/:id/books
。
考虑我们不关心所有书籍的情况 来自特定作者的书籍清单(有效地使#1号路线无法使用)。
这使得BooksController#index
的逻辑类似于:
# BooksController.rb
def index
@books = Book.where(author: author)
render json: @books
end
然而,Author
作用范围让我感到非常不舒服,因为它是一般BooksController
,其他CRUD方法与Authors
无关。 上述#index方法应该在Author::BooksController
之类的单独的命名空间控制器中吗?