Rails 常见路由

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_pathsearch_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
    原文作者:天赢金创
    原文地址: https://segmentfault.com/a/1190000000536955
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞