动物庄园pdf百度云:Rails ---> routes.rb 详解

来源:百度文库 编辑:中财网 时间:2024/05/10 09:33:10

Rails ---> routes.rb 详解  

2011-09-01 15:48:05|  分类: 动态语言|字号 订阅

http://hi.baidu.com/kangjun118/blog/item/5f960cde0545bb2e32fa1c5d.html

理解路由的目的

看懂routes.rb文件中的代码

使用经典的hash风格或者现在比较流行的Restful风格构造你自己的路径

断定一个路径会映射到哪一个controller和action

路由的双重作用

Rails的路由是一个双重的机制 - 你既能把树木变成纸张,也能把纸张变成树木.更准确的说,它既能将进入服务器的HTTP请求连接到你的controller,也能帮助你(在View内)生成URL而不需要使用硬编码的字符串.

从URL到代码

当你的Rails应用接收到HTTP请求后,比如:

GET /patients/17

Rails的路由引擎就是把请求分发到你的应用中合适点的那些代码.具体到这个例子,应用程序比较可能会运行patients controller中的show action,并展示id是17的patient的详细信息.

从代码到URL

路由也可以反过来作用.如果你的应用包含了以下代码

@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/photosPhotosindexdisplay a list of all photosGET/photos/newPhotosnewreturn anHTMLform for creating a new photoPOST/photosPhotoscreatecreate a new photoGET/photos/1Photosshowdisplay a specific photoGET/photos/1/editPhotoseditreturn anHTMLform for editing a photoPUT/photos/1Photosupdateupdate a specific photoDELETE/photos/1Photosdestroydelete 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/newGeocodersnewreturn anHTMLform for creating the new geocoderPOST/geocoderGeocoderscreatecreate the new geocoderGET/geocoderGeocodersshowdisplay the one and only geocoder resourceGET/geocoder/editGeocoderseditreturn anHTMLform for editing the geocoderPUT/geocoderGeocodersupdateupdate the one and only geocoder resourceDELETE/geocoderGeocodersdestroydelete 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命名空间和路由


默认路由:

Ruby代码# Rails3:  match '/:controller(/:action(/:id))'  # Rails2:  map.connect ':controller/:action/:id'  # Rails3: match '/:controller(/:action(/:id))' # Rails2: map.connect ':controller/:action/:id' 

正则路由:
Ruby代码# 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'

命名路由:
Ruby代码# 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 => ''

根路由:
Ruby代码# Rails3:  root => 'welcome#show'  # Rails2:  map.root :controller => 'welcome', :action => 'show'  # Rails3: root => 'welcome#show' # Rails2: map.root :controller => 'welcome', :action => 'show'

路由简写技巧:
:to 键的省略: 
Ruby代码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)一至时,可省略指派控制器部分 
Ruby代码match 'account/overview'  # 相当于:  match 'account/overview', :to => 'account#overview'  match 'account/overview' # 相当于: match 'account/overview', :to => 'account#overview' 

Verb路由
当需要限制http请求方法的时候通过键 :via ,也可以直接把方法写在最前面: 
Ruby代码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路由:
Ruby代码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中一致 
Ruby代码resources :blogs, :shallow => true do    resources :comments  end  resources :blogs, :shallow => true do resources :comments end 
使用:shallow前后相同部分:
blog_commentsGET/blogs/:blog_id/comments(.:format){:controller=>"comments", :action=>"index"}blog_commentsPOST/blogs/:blog_id/comments(.:format){:controller=>"comments", :action=>"create"}new_blog_commentGET/blogs/:blog_id/comments/new(.:format){:controller=>"comments", :action=>"new"}blogsGET/blogs(.:format){:controller=>"blogs", :action=>"index"}blogsPOST/blogs(.:format){:controller=>"blogs", :action=>"create"}new_blogGET/blogs/new(.:format){:controller=>"blogs", :action=>"new"}edit_blogGET/blogs/:id/edit(.:format){:controller=>"blogs", :action=>"edit"}blogGET/blogs/:id(.:format){:controller=>"blogs", :action=>"show"}blogPUT/blogs/:id(.:format){:controller=>"blogs", :action=>"update"}blogDELETE/blogs/:id(.:format){:controller=>"blogs", :action=>"destroy"}


使用:shallow前后不同部分:
不使用shallow选项:
edit_blog_commentGET/blogs/:blog_id/comments/:id/edit(.:format){:controller=>"comments", :action=>"edit"}blog_commentGET/blogs/:blog_id/comments/:id(.:format){:controller=>"comments", :action=>"show"}blog_commentPUT/blogs/:blog_id/comments/:id(.:format){:controller=>"comments", :action=>"update"}blog_commentDELETE/blogs/:blog_id/comments/:id(.:format){:controller=>"comments", :action=>"destroy"}

使用shallow选项后:
edit_commentGET/comments/:id/edit(.:format){:controller=>"comments", :action=>"edit"}commentGET/comments/:id(.:format){:controller=>"comments", :action=>"show"}commentPUT/comments/:id(.:format){:controller=>"comments", :action=>"update"}commentDELETE/comments/:id(.:format){:controller=>"comments", :action=>"destroy"}
可以看出使用shallow选项后,对于已经存在的资源使用简化方式操作,具体行为涉及到 edit\show\update\destroy 四种 
另外,shallow选项的有效范围是对自身及嵌套的资源都有效,如下面这个例子: 
Ruby代码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
Ruby代码

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' 

Ruby代码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' 

Ruby代码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 


在路由中定义跳转:
Ruby代码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}" } 

路由中的限制:
Ruby代码# 限制 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+/} 

路由通配符:
Ruby代码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:
Ruby代码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"]] }