开发者

working with resources that are both nested/non-nested

Suppose I have the following models/relationships in my Rails app:

class Book < ActiveRecord::Base
  has_many :reviews
end

class Review < ActiveRecord::Base
  belongs_to :book
end

and I want reviews to be both a standalone resource and a nested resource of Books:

resources :books do
  resources :reviews
end

resources :reviews

For example, I want "/reviews" to display the most recent reviews (over all books), and "/book/1/reviews" to display the most recent reviews of book 1.

What's the best way to handle this in my controller and views? For example, do I have a single index action and a single index view, and use some conditional statements to check whether I want to display all reviews or only reviews for a particular book? Or do I have separate all_inde开发者_如何学Gox and particular_book_index actions and views?


I recommend Inherited Resources.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜