资源路由,方便阅读,易拓展(添加额外的action,进行不同的功能处理)
action的类型: 七个基本action中,index/show/new/edit都是渲染一个页面,
而create/update/destroy是只完成功能,这类action在结尾往往会进行重定向或返回json/html的动作,将请求转向其他地方。接受请求,完成功能,并将渲染动作交给第一类action。
资源路由拓展:集合(collection)路由 和 成员(member)路由
- 集合(collection)路由:localhost:3000/posts/recent 会相应这个路由
resources :posts do
get 'recent', :on => :collection
end
get :on 关键字 => :类型(collection)
这样会生成对应的命名路由
resources :posts do
collection do
get 'recent'
end
end
这样就可以批量的添加集合路由了
2. 成员(member)路由: localhost:3000/posts/:id/recent 会相应这个路由
当需要响应和某一条记录有关的时候,就用这个
resources :posts do
member do
get 'recent'
end
end
与集合路由类似的用法,