Rails Routescheatsheet
贡献者:BAI
资源(Resource)
Resources
RUBYresources :photos# PhotosController:# index => GET /photos# new => GET /photos/new# create => POST /photos/new# show => GET /photos/:id# edit => GET /photos/:id/edit# update => PUT /photos/:id# delete => DELETE /photos/:id## Helpers:# new_photo_path# photo_path(id)# edit_photo_path(id)
自定义 action
RUBYresources :photos do member { get 'preview' } # /photo/1/preview collection { get 'search' } # /photos/search get 'preview', on: :member # 和第一个相同end
Options
RUBYresources :photos, path_names: { new: 'brand_new' } # /photos/1/brand_new path: 'postings' # /postings only: :index only: [:index, :show] except: :show except: [:index, :show] shallow: true # 会生成 shallow 路由 shalow_path: 'secret' shallow_prefix: 'secret'
单独 resource (resource
)
RUBYresource :coder# CodersController:# new => GET /coder/new# create => POST /coder/new# show => GET /coder# edit => GET /coder/edit# update => PUT /coder# delete => DELETE /coder
Match (match
)
RUBYmatch 'photo/:id' => 'photos#show' # /photo/what-is-itmatch 'photo/:id', id: /[0-9]+/ # /photo/0192match 'photo/:id' => 'photos#show', constraints: { id: /[0-9]+/ }match 'photo/:id', via: :getmatch 'photo/:id', via: [:get, :post]match 'photo/*path' => 'photos#unknown' # /photo/what/ever# params[:format] == 'jpg'match 'photos/:id' => 'photos#show', :defaults => { :format => 'jpg' }
Get/post
get
等同于 match via: :get
.
RUBYget 'photo/:id' => 'photos#show'# 同 match 'photo/:id' => 'photos#show', via: :getpost 'photo/:id' => 'photos#update'# 同 match 'photo/:id' => 'photos#show', via: :post
重定向
RUBYmatch '/stories' => redirect('/posts')match '/stories/:name' => redirect('/posts/%{name}')
命名
RUBY# logout_pathmatch 'exit' => 'sessions#destroy', as: :logout
约束(Constraint)
RUBYmatch '/', constraints: { subdomain: 'admin' }# admin.site.com/admin/photosnamespace 'admin' do constraints subdomain: 'admin' do resources :photos endend
自定义约束
RUBYclass BlacklistConstraint def initialize @ips = Blacklist.retrieve_ips end def matches?(request) @ips.include?(request.remote_ip) endendTwitterClone::Application.routes.draw do match "*path" => "blacklist#index", :constraints => BlacklistConstraint.newend
Scope
RUBYscope 'admin', constraints: { subdomain: 'admin' } do resources ...end
Rack 中间件
RUBY# Yes, Sprockets is middlewarematch '/application.js' => Sprockets
Route 帮助方法
RUBYprojects_path # /projectsprojects_url # http://site.com/projects
完整示例
RUBY# 路由的顺序也是下面路由创建的顺序# 第一个创建的路由也有最高的优先级# 标准路由match 'products/:id' => 'catalog#view'# :controller 和 :action 可以也换成其他的值# 给路由命名match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase# 上面的路由可以通过 purchase_url(:id => product.id) 调用resources :productsresources :products do member do get 'short' post 'toggle' end collection do get 'sold' endend# 子资源resources :products do resources :comments, :sales resource :sellerend# 更复杂的子资源resources :products do resources :comments resources :sales do get 'recent', :on => :collection endend# 带命名空间namespace :admin do # 路由为 /admin/products/* , 对应的 Controller 为 Admin::ProductsController # 文件为 (app/controllers/admin/products_controller.rb) resources :productsendroot :to => 'welcome#index'# "rake routes" 可以查看全部路由信息