mysql,Linux,HighPerformance,ruby on Rails

2009年10月9日星期五

route里的:member和:collection是怎么用的

以下内容基于rails,route里经常会用到collection和member,但已经忘了它是何物了,可见不练的后果。查之,

这里的对话可以直接让我明白,相信你也会明白.
总结下来,想在RESTful的7种action之外再增加其它action,就需要使用:collection,如果想针对单个object作操作,请使用:member.

RubyHunt: What’s the use of the :collection and :member methods within a route? What’s the difference between the two?
melvinram: Collection adds routes to the entire collection
RubyHunt: So the collection is used to add another action inside the controller
melvinram: If you had a resource of Contacts, a collection route you might add would be delete_all_older_than_6_months
melvinram: and the route to get to it would be be /contacts/delete_all_older_than_6_months/
RubyHunt: Which otherwise would be restricted to only 7 actions ?
melvinram: yes
melvinram: A member method applies only to one record. For example, you might add a member method called upgrade_to_vip that would update one record to have a status of VIP or something…
melvinram: And the route would be /contacts/1/upgrade_to_vip
RubyHunt: So the :collection is used to add action inside a RESTful controller and the :member would be used to apply action for individual object.

没有评论:

发表评论