理解路由的目的javascript
看懂routes.rb文件中的代码html
使用经典的hash风格或者如今比较流行的Restful风格构造你本身的路径java
判定一个路径会映射到哪个controller和actiongit
路由的双重做用github
Rails的路由是一个双重的机制 - 你既能把树木变成纸张,也能把纸张变成树木.更准确的说,它既能将进入服务器的HTTP请求链接到你的controller,也能帮助你(在View内)生成URL而不须要使用硬编码的字符串.web
从URL到代码
当你的Rails应用接收到HTTP请求后,好比:数据库
GET /patients/17安全
Rails的路由引擎就是把请求分发到你的应用中合适点的那些代码.具体到这个例子,应用程序比较可能会运行patients controller中的show action,并展现id是17的patient的详细信息.服务器
从代码到URL
路由也能够反过来做用.若是你的应用包含了如下代码session
@patient = Patient.find(17)
<%= link_to “Patient Record”, patient_path(@patient) %>
那么,路由引擎就会把这段代码解释成这样的URL: http://example.com/patients/17.这样使用路由,比起硬编码URL,能够下降你的应用程序的脆弱程度(增长程序健壮性),而且能够增长代码的可读性,使你的程序更容易被理解.
Patient须要被声明成为resource才可使用这种形式的路径转换
快速浏览Routes.rb
Rails的路由有两个组陈部分:做为Rails一部分的路由引擎,还有包含有你的应用程序真正路径的config/routes.rb文件.讲授能够把什么东西放到routes.rb里面去是本文的目的,不过在此以前咱们先概览一下.
处理文件
从格式上讲,routes.rb只不过是被传递给ActionController::Routing:routes.draw的一个大大的 block. 这个文件里也能够有注释,不过大多数状况下,应该是移行移行的代码,每一行表明你应用程序的一条路径.路径有五种主要类型:
RESTful路径 命名路径 嵌套路径 常规路径 默认路径
每个类型的路径都会在随后详细阐述. 当有请求进入时, routes.rb文件自顶向下出路.请求会被分发到第一个匹配的路径.若是没有匹配的路径,Rails会返回给调用者一个HTTP 404.
RESTful路径
RESTful路径沾了Rails内建REST机制的光,只用一句单独的声明就包含了不少路由信息.一个RESTful路径看起来是这样的:
map.resources :books
命名路径
命名路径在处理请求的同时,也能够给你的代码提供很是具备可读性的连接.这是一个典型的命名路径:
map.login ‘/login’, :controller => ’sessions’, :action => ‘new’
嵌套路径
嵌套路径可让你在一个resource里面包含另外一个resource.你待会儿就会知道它是怎么被转化成URL(绝对路径 http://niuwa.org/index.html)和path(相对路径 /index.html)的.举例来讲,若是你的应用包含不少部分,每一个部分属于一个包(assembly),你也许能够这么写:
map.resources :assemblies do |assemblies| assemblies.resources :parts end
常规路径
在不少应用中,你会看到非RESTful的路由,它们显式的连接URL和action.例如:
map.connect ‘parts/:number’, :controller => ‘inventory’, :action => ’show’
默认路径
默认路径十一个安全网,抓住那些没有匹配上其余路径的请求.不少Rails应用都会包含这两条默认路径:
map.connect ‘:controller/:action/:id’ map.connect ‘:controller/:action/:id.:format’
这两个默认路径是你建立Rails应用的时候自动生成的.你能够为你的应用里面的全部东西使用RESTful的路径,那你就有可能想去掉这两条默认路径.在你去掉它们以前请确保你没有用到它们.
`
RESTful路由:Rails的默认方式
RESTful路由是目前Rails的标准路由方式,而且它也是你在新建的应用中首先使用的方式.要理解RESTful路有可能要花上一点时间,不 过这样 的努力是值得的;你的代码会变得更容易阅读,而且当你使用这种路由方式的时候,你会跟Rails配合的很好,而不是跟Rails对着干.
什么是REST?
RESTful 路由的基础是由Roy Fielding的博士论文提出的,参见: Architectural Styles and the Design of Network-based Software Architectures. 幸运的是,你不须要看懂整篇博士论文就能够理解Rails中的REST是怎么回事.REST是Representational State Transfer(基于表现的状态转移)的缩写.对咱们来讲,主要有两点:
使用资源定位符(对咱们来讲就是URL)来表示资源 在系统的不一样组件之间转移表现状态
举例来讲,对于Rails应用中这样的一个请求:
DELETE /phote/17
会被理解成是操做ID是17的photo,而且指示出了指望的操做 - 删除该资源.REST是web应用架构的一种天然的方式,而且Rails进一步使用约定(convention)隐藏了RESTful的某些复杂性使之更加天然(大概是指用起来更简单).
CRUD,动词和action
在RAils中,一个RESTful路径提供了HTTP动词(也就是GET, PUT等等),controller actions和(隐含的)CRUD数据库操做之间的的映射.路由文件中一个单独的入口以下所示:
map.resources :photos
在你的应用中建立了七个不一样的路径:
|
|
|
|
|
GET |
/photos |
Photos |
index |
display a list of all photos |
GET |
/photos/new |
Photos |
new |
return anHTMLform for creating a new photo |
POST |
/photos |
Photos |
create |
create a new photo |
GET |
/photos/1 |
Photos |
show |
display a specific photo |
GET |
/photos/1/edit |
Photos |
edit |
return anHTMLform for editing a photo |
PUT |
/photos/1 |
Photos |
update |
update a specific photo |
DELETE |
/photos/1 |
Photos |
destroy |
delete a specific photo |
对这些路径(由以前的resource定义生成的),resource的id能够在相应的controller action中使用params[:id]获得.
若是你一直在你的应用程序里面使用RESTful路径,你应该删除routes.rb中的默认路径,这样就会迫使Rails使用HTTP动词和路径间的映射.
URL和Path
建立RESTful路径会在你的应用程序里面生成一堆helper:
photos_url 和photos_path映射到index和create两个action new_photo_url和new_photo_path映射到new action edit_photo_url和edit_photo_path映射到edit action photo_url和photo_path映射到show, update和destroy三个action
由于路由同时使用HTTP动词和path两者分发请求,所以这七个RESTful路由生成的路径只有4对helper.
在这里,以_url结尾的helper生成应用程序能理解的整个URL(包含主机名),而以_path结尾的helper仅生成从应用程序根目录开始的path(不包含主机名).例如:
photos_url# => “http://www.example.com/photos”
photos_path# => “/photos”
同时定义多个资源
若是你须要为多个资源建立路径,你可使用一次map.resources调用就定义它们:
map.resources :photos, :books, :videos
跟这样定义的效果是同样的:
map.resources :photos map.resources :books map.resources :videos
单数形式资源
你也能够应用RESTful路由定义单数形式的资源.这是,你须要使用map.resource代替刚才的map.resources,这时生成的路径会略有不一样:
map.resource :geocoder
建立六个不一样的路径:
|
|
|
|
|
GET |
/geocoder/new |
Geocoders |
new |
return anHTMLform for creating the new geocoder |
POST |
/geocoder |
Geocoders |
create |
create the new geocoder |
GET |
/geocoder |
Geocoders |
show |
display the one and only geocoder resource |
GET |
/geocoder/edit |
Geocoders |
edit |
return anHTMLform for editing the geocoder |
PUT |
/geocoder |
Geocoders |
update |
update the one and only geocoder resource |
DELETE |
/geocoder |
Geocoders |
destroy |
delete the geocoder resource |
虽然routes.rb中的资源名称是单数,可是对应的controller名称依然是复数.
一个单数形式的RESTful路径生成以下的helper:
new_geocoder_url 和new_geocoder_path映射到new action edit_geocoder_url和edit_geocoder_path映射到edit action geocoder_url和geocode_path映射到create, update和destroy三个action 自定义资源
虽然使用RESTful路由约定对不少应用来讲基本就足够了,不过仍是有一些其余的方法自定义RESTful路径的工做方式.这些选项包括:
:controller :singular :requirements :conditions :as :path_names :path_prefix :name_prefix :only :except
你也可使用:member和:collection选项添加路径,这个在本文中稍后讨论.
使用:controller
:controller选项使你可使用和公开的资源名称不一样的controller名称,例如:
map.resources :photos, :controller => “images”
生成helper时会依据资源的名称,
而不是controller的名称.所以,在这个例子里,就会获得photos_path,new_photo_path等等.
controller命名空间和路由
默认路由:
# Rails3:
match '/:controller(/:action(/:id))'
# Rails2:
map.connect ':controller/:action/:id'
# Rails3: match '/:controller(/:action(/:id))' # Rails2: map.connect ':controller/:action/:id'
正则路由:
# Rails3:
match 'products/:id', :to => 'catalog#view'
# Rails2:
map.connect 'products/:id', :controller => 'catalog', :action => 'view'
# Rails3: match 'products/:id', :to => 'catalog#view' # Rails2: map.connect 'products/:id', :controller => 'catalog', :action => 'view'
命名路由:
# Rails3:
match 'logout', :to => 'sessions#destroy', :as => 'logout'
# Rails2:
map.logout 'logout', :controller => 'sessions', :action => ''
# Rails3: match 'logout', :to => 'sessions#destroy', :as => 'logout' # Rails2: map.logout 'logout', :controller => 'sessions', :action => ''
根路由:
# Rails3:
root => 'welcome#show'
# Rails2:
map.root :controller => 'welcome', :action => 'show'
# Rails3: root => 'welcome#show' # Rails2: map.root :controller => 'welcome', :action => 'show'
路由简写技巧:
:to 键的省略:
match 'account' => 'account#index'
# 至关于:
match 'account', :to => 'account#index'
match 'info' => 'projects#info', :as => 'info'
match 'account' => 'account#index' # 至关于: match 'account', :to => 'account#index' match 'info' => 'projects#info', :as => 'info'
注意:
:as 在rails3中是改变 helper, 在rails2中是改变 path
当路径和控制器(及action)一至时,可省略指派控制器部分
match 'account/overview'
# 至关于:
match 'account/overview', :to => 'account#overview'
match 'account/overview' # 至关于: match 'account/overview', :to => 'account#overview'
Verb路由
当须要限制http请求方法的时候经过键
:via ,也能够直接把方法写在最前面:
get 'account/overview'
# 至关于:
match 'account/overview', :via => 'get'
match 'account/setup', :via => [:get, :post]
# 支持get\post\put\delete四种HTTP方法
get 'account/overview' # 至关于: match 'account/overview', :via => 'get' match 'account/setup', :via => [:get, :post] # 支持get\post\put\delete四种HTTP方法
resources路由:
resources :posts, :except => [:index]
resources :posts, :only => [:new, :create]
# edit_post GET /posts/:id/modify(.:format) {:controller=>"posts", :action=>"edit"}
resources :posts, :path_names => { :edit => 'modify' }
resources :projects do
resources :tasks, :people
end
resources :products do
collection do
get :sold
post :on_offer, :search
end
get :buy, :on => :member
post :batch, :on => :collection
end
resource :session do
get :create
end
resources :posts, :except => [:index] resources :posts, :only => [:new, :create] # edit_post GET /posts/:id/modify(.:format) {:controller=>"posts", :action=>"edit"} resources :posts, :path_names => { :edit => 'modify' } resources :projects do resources :tasks, :people end resources :products do collection do get :sold post :on_offer, :search end get :buy, :on => :member post :batch, :on => :collection end resource :session do get :create end
:shallow用法:
Rails3中的shallow用法与Rails2中一致
resources :blogs, :shallow => true do
resources :comments
end
resources :blogs, :shallow => true do resources :comments end
使用:shallow先后相同部分:
blog_comments |
GET |
/blogs/:blog_id/comments(.:format) |
{:controller=>"comments", :action=>"index"} |
blog_comments |
POST |
/blogs/:blog_id/comments(.:format) |
{:controller=>"comments", :action=>"create"} |
new_blog_comment |
GET |
/blogs/:blog_id/comments/new(.:format) |
{:controller=>"comments", :action=>"new"} |
blogs |
GET |
/blogs(.:format) |
{:controller=>"blogs", :action=>"index"} |
blogs |
POST |
/blogs(.:format) |
{:controller=>"blogs", :action=>"create"} |
new_blog |
GET |
/blogs/new(.:format) |
{:controller=>"blogs", :action=>"new"} |
edit_blog |
GET |
/blogs/:id/edit(.:format) |
{:controller=>"blogs", :action=>"edit"} |
blog |
GET |
/blogs/:id(.:format) |
{:controller=>"blogs", :action=>"show"} |
blog |
PUT |
/blogs/:id(.:format) |
{:controller=>"blogs", :action=>"update"} |
blog |
DELETE |
/blogs/:id(.:format) |
{:controller=>"blogs", :action=>"destroy"} |
|
使用:shallow先后不一样部分:
不使用shallow选项:
edit_blog_comment |
GET |
/blogs/:blog_id/comments/:id/edit(.:format) |
{:controller=>"comments", :action=>"edit"} |
blog_comment |
GET |
/blogs/:blog_id/comments/:id(.:format) |
{:controller=>"comments", :action=>"show"} |
blog_comment |
PUT |
/blogs/:blog_id/comments/:id(.:format) |
{:controller=>"comments", :action=>"update"} |
blog_comment |
DELETE |
/blogs/:blog_id/comments/:id(.:format) |
{:controller=>"comments", :action=>"destroy"} |
使用shallow选项后:
edit_comment |
GET |
/comments/:id/edit(.:format) |
{:controller=>"comments", :action=>"edit"} |
comment |
GET |
/comments/:id(.:format) |
{:controller=>"comments", :action=>"show"} |
comment |
PUT |
/comments/:id(.:format) |
{:controller=>"comments", :action=>"update"} |
comment |
DELETE |
/comments/:id(.:format) |
{:controller=>"comments", :action=>"destroy"} |
能够看出使用shallow选项后,对于已经存在的资源使用简化方式操做,具体行为涉及到 edit\show\update\destroy 四种
另外,shallow选项的有效范围是对自身及嵌套的资源都有效,以下面这个例子:
resources :publishers do
resources :magazines do
resources :albums, :shallow => true do
resources :photos do
resources :images
end
end
end
end
resources :publishers do resources :magazines do resources :albums, :shallow => true do resources :photos do resources :images end end end end
这个例子中 albums、photos、images 都会使用简化方式,而 magazines 不会。特别注意:这种嵌套方式极不推荐,通常嵌套的层级最好不要超过一级
scope路由
:path 改变Path,:module 改变Controller, :name_prefix || :as 改变 helper
scope 'admin' do resources :posts end # 行当于: scope :path => 'admin' do resources :posts end
scope 'admin' do resources :posts end # 行当于: scope :path => 'admin' do resources :posts end
Ruby代码
scope :module => 'admin' do
resources :posts
end
# 至关于:
resources :posts, :module => 'admin'
scope :module => 'admin' do resources :posts end # 至关于: resources :posts, :module => 'admin'
scope :name_prefix => 'admin' do
resources :posts
end
# 至关于:
resources :posts, :name_prefix => 'admin'
scope :name_prefix => 'admin' do resources :posts end # 至关于: resources :posts, :name_prefix => 'admin'
scope 'admin', :module => 'admin', :name_prefix => 'admin' do
resources :posts
end
# 至关于:
namespace 'admin' do
resources :posts
end
scope 'admin', :module => 'admin', :name_prefix => 'admin' do resources :posts end # 至关于: namespace 'admin' do resources :posts end
在路由中定义跳转:
match "/posts/github" => redirect("http://github.com/rails.atom")
# 地址 /foo/1 会自动跳转到 /bar/1s
match "/foo/:id", :to => redirect("/bar/%{id}s")
# /account/proc/inosin 会自动跳转到 /inosins
match 'account/proc/:name', :to => redirect {|params|
"/#{params[:name].pluralize}" }
match "/stories" => redirect {|p, req| "/posts/#{req.subdomain}" }
match "/posts/github" => redirect("http://github.com/rails.atom") # 地址 /foo/1 会自动跳转到 /bar/1s match "/foo/:id", :to => redirect("/bar/%{id}s") # /account/proc/inosin 会自动跳转到 /inosins match 'account/proc/:name', :to => redirect {|params| "/#{params[:name].pluralize}" } match "/stories" => redirect {|p, req| "/posts/#{req.subdomain}" }
路由中的限制:
# 限制 id 只能为数字
match "/posts/show/:id", :to => "posts#index", :id => /\d+/
match "/posts/show/:id", :to => "posts#index", :constraints => {:id => /\d+/}
# 限制子域名
match "photos", :constraints => {:subdomain => "admin"}
# 限制访问者 IP
constraints(:ip => /127.0.0.1/) do
match '/questions', :to => redirect("http://www.stackoverflow.com/")
end
# 当访问者 ip 是 192.168.1.* 的来访者访问 子域名为 "test"
match "/ttt" => proc{|env| [200, {}, ["hello test"]]}, \
:constraints => {:subdomain => "test", :ip => /192\.168\.1\.\d+/}
# 限制 id 只能为数字 match "/posts/show/:id", :to => "posts#index", :id => /\d+/ match "/posts/show/:id", :to => "posts#index", :constraints => {:id => /\d+/} # 限制子域名 match "photos", :constraints => {:subdomain => "admin"} # 限制访问者 IP constraints(:ip => /127.0.0.1/) do match '/questions', :to => redirect("http://www.stackoverflow.com/") end # 当访问者 ip 是 192.168.1.* 的来访者访问 子域名为 "test" match "/ttt" => proc{|env| [200, {}, ["hello test"]]}, \ :constraints => {:subdomain => "test", :ip => /192\.168\.1\.\d+/}
路由通配符:
resources :photos, :id => /\d+/
match 'photos/*other' => 'photos#unknown'
#上面这两行路由则会把不符合7种path的其余url所有解析到PhotoController#unknown中去处理,params[:other]可获得path中/photos/以后的部分,注意这两行的顺序不能颠倒
match 'books/*section/:title' => 'books#show'
# 例如:books/some/section/last-words-a-memoir 中 params[:section] = "some/section", params[:title] = "last-words-a-memoir".
match '*a/foo/*b' => 'test#index'
# 例如:zoo/woo/foo/bar/baz 中 params[:a] = "zoo/woo", params[:b] = "bar/baz"
resources :photos, :id => /\d+/ match 'photos/*other' => 'photos#unknown' #上面这两行路由则会把不符合7种path的其余url所有解析到PhotoController#unknown中去处理,params[:other]可获得path中/photos/以后的部分,注意这两行的顺序不能颠倒 match 'books/*section/:title' => 'books#show' # 例如:books/some/section/last-words-a-memoir 中 params[:section] = "some/section", params[:title] = "last-words-a-memoir". match '*a/foo/*b' => 'test#index' # 例如:zoo/woo/foo/bar/baz 中 params[:a] = "zoo/woo", params[:b] = "bar/baz"
Rack:
match "/foo", :to => proc {|env| [200, {}, ["Hello world"]] }
match 'rocketeer.js' => ::TestRoutingMapper::RocketeerApp
RocketeerApp = lambda { |env|
[200, {"Content-Type" => "text/html"}, ["javascripts"]]
}
match "/foo", :to => proc {|env| [200, {}, ["Hello world"]] } match 'rocketeer.js' => ::TestRoutingMapper::RocketeerApp RocketeerApp = lambda { |env| [200, {"Content-Type" => "text/html"}, ["javascripts"]] }