Member
声明一个动作属于但个资源的URI,例如:/photos/1/preview进行get,有preview_photo_path(photo)
和preview_photo_url(photo)
这样的UrlHelper可以使用
resources :photos do
member do
get 'preview'
end
end
或者使用
resources :phtotos do
get 'preview', on: :member
end
Collection
声明一个动作属于一组资源的URI,例如:/photos/search进行get,有search_photos_path
或 search_photos_url
这样的 UrlHelper 可以用。
resources :photos do
collection do
get 'search'
end
end
也可以使用
resources :photos do
get 'search', on: :collection
end
Nested Resources
Namespace Reources
resources :posts,only: [:show]
namespace :admin do
resources :posts,except: [:show]
end
生成的url如下:
Prefix Verb URI Pattern Controller#Action
post GET /posts/:id(.:format) posts#show
admin_posts GET /admin/posts(.:format) admin/posts#index
POST /admin/posts(.:format) admin/posts#create
new_admin_post GET /admin/posts/new(.:format) admin/posts#new
edit_admin_post GET /admin/posts/:id/edit(.:format) admin/posts#edit
admin_post PATCH /admin/posts/:id(.:format) admin/posts#update
PUT /admin/posts/:id(.:format) admin/posts#update
DELETE /admin/posts/:id(.:format) admin/posts#destroy