开发者

Most Rails-y way to give different views of the same resource?

In Rails, is there a canonical way of giving different views of the same resource?

For example, a directory of people, where each person can have multiple photos, phone numbers, email addresses, etc. The people, photos and phone numbers are actually different resources with their own RESTful actions.

But when viewing people, one page might shows everyone's name and associated photos; another page is names and associated contact information, formatted for printing.

Would it be开发者_如何转开发 more "Rails-y" to:

  • Create additional actions on the People controller besides the RESTful ones, like "index_with_contact_info"?
  • Create a different controller and a different group of views?

Neither seems quite right to me, but the first seems more likely. Any thoughts?


I think this situation is ripe for using nested resources. person/:person/photos/ could point to the photos of a person and so on. There is a lot of material on using nested resources that you might want to check out. In short,

  map.resources :people, :has_many => { :photos, :contacts  }, :shallow => true

or some variation of it in your routes and then define the photos and contacts controllers assuming availability of the person_id.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜