开发者

working with resources that are both nested/non-nested

开发者 https://www.devze.com 2023-01-27 03:18 出处:网络
Suppose I have the following models/relationships in my Rails app: class Book < ActiveRecord::Base has_many :reviews

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

精彩评论

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