Welcom ,I hope yo will enjoy here!

rails之路由

  1. 理解路由的目的
  2. 看懂routes.rb文件中的代码
  3. 使用经典的hash风格或者现在比较流行的Restful风格构造你自己的路径
  4. 断定一个路径会映射到哪一个controller和action


1. 路由的双重作用

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

    1.1 从URL到代码

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

   GET /patients/17

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

    1.2 从代码到URL

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

    @patient = Patient.find(17)

    <%= link_to “Patient Record”, patient_path(@patient) %>

    那么,路由引擎就会把这段代码解释成这样的URL: http://example.com/patients/17.这样使用路由,比起硬编码URL,可以降低你的应用程序的脆弱程度(增加程序健壮性),并且可以增加代码的可读性,使你的程序更容易被理解.

    Patient需要被声明成为resource才可以使用这种形式的路径转换

 

2. 快速浏览Routes.rb

    Rails的路由有两个组陈部分:作为Rails一部分的路由引擎,还有包含有你的应用程序真正路径的config/routes.rb文件.讲授可以把什么东西放到routes.rb里面去是本文的目的,不过在此之前我们先概览一下.

    2.1 处理文件

    从格式上讲,routes.rb只不过是被传递给ActionController::Routing:routes.draw的一个大大的 block. 这个文件里也可以有注释,不过大多数情况下,应该是y一行一行的代码,每一行代表你应用程序的一条路径.

    路径有五种主要类型: RESTful路径 命名路径 嵌套路径 常规路径 默认路径

   每一个类型的路径都会在随后详细阐述. 当有请求进入时, routes.rb文件自顶向下出路.请求会被分发到第一个匹配的路径.如果没有匹配的路径,Rails会返回给调用者一个HTTP 404.

  

  2.1.1 RESTful路径

   RESTful路径沾了Rails内建REST机制的光,只用一句单独的声明就包含了很多路由信息.一个RESTful路径看起来是这样的:

   map.resources :books

 

   2.1.2 命名路径

   命名路径在处理请求的同时,也可以给你的代码提供非常具有可读性的链接.这是一个典型的命名路径:

   map.login ‘/login’, :controller => ’sessions’, :action => ‘new’

 

  2.1.3 嵌套路径

   嵌套路径可以让你在一个resource里面包含另一个resource.你待会儿就会知道它是怎么被转化成URL(绝对路径 http://niuwa.org/index.html)和path(相对路径 /index.html)的.举例来说,如果你的应用包含很多部分,每个部分属于一个包(assembly),你也许可以这么写:

   map.resources :assemblies do |assemblies|

         assemblies.resources :parts

   end

 

   2.1.4 常规路径

   在很多应用中,你会看到非RESTful的路由,它们显式的链接URL和action.例如:

map.connect ‘parts/:number’, :controller => ‘inventory’, :action => ’show’

 

   2.1.5 默认路径

   默认路径,抓住那些没有匹配上其他路径的请求.很多Rails应用都会包含这两条默认路径:

   map.connect ‘:controller/:action/:id’

   map.connect ‘:controller/:action/:id.:format’

 

   这两个默认路径是你创建Rails应用的时候自动生成的.你可以为你的应用里面的所有东西使用RESTful的路径,那你就有可能想去掉这两条默认路径.在你去掉它们之前请确保你没有用到它们.

 

3 RESTful路由:Rails的默认方式

    RESTful路由是目前Rails的标准路由方式,并且它也是你在新建的应用中首先使用的方式.要理解RESTful路有可能要花上一点时间,不过这样 的努力是值得的;你的代码会变得更容易阅读,并且当你使用这种路由方式的时候,你会跟Rails配合的很好,而不是跟Rails对着干.

   3.1 什么是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的某些复杂性使之更加自然(大概是指用起来更简单).

    3.2 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动词和路径间的映射.

  

   3.3 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”

 

   3.4 同时定义多个资源

   如果你需要为多个资源创建路径,你可以使用一次map.resources调用就定义它们:

   map.resources :photos, :books, :videos

   跟这样定义的效果是一样的:

   map.resources :photos

   map.resources :books

    map.resources :videos

 

   3.5 单数形式资源

   你也可以应用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选项添加路径,这个在本文中稍后讨论.

  

  3.6 使用:controller

   :controller选项使你可以使用和公开的资源名称不同的controller名称,例如:

    map.resources :photos, :controller => “images”

   生成helper时会依据资源的名称,而不是controller的名称.因此,在这个例子里,就会得到photos_path,new_photo_path等等.

 

  4. controller命名空间和路由


    4.1 默认路由:

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

   4.2 正则路由:

   # Rails3: match 'products/:id', :to => 'catalog#view' # Rails2: map.connect 'products/:id', :controller => 'catalog', :action => 'view'

 

   4.3 命名路由:

  # Rails3: match 'logout', :to => 'sessions#destroy', :as => 'logout' # Rails2: map.logout 'logout', :controller => 'sessions', :action => ''

 

   4.4 根路由: 

  # Rails3: root => 'welcome#show' # Rails2: map.root :controller => 'welcome', :action => 'show'


   5. 路由简写技巧:


    5.1 :to 键的省略:

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'

      5.2 Verb路由
    当需要限制http请求方法的时候通过键 :via ,也可以直接把方法写在最前面:

 

    get 'account/overview' # 相当于: match 'account/overview', :via => 'get'

    match 'account/setup', :via => [:get, :post]  # 支持get\post\put\delete四种HTTP方法


6. 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


   6.1  :shallow用法:
   Rails3中的shallow用法与Rails2中一致

    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
这个例子中 albums、photos、images 都会使用简化方式,而 magazines 不会。特别注意:这种嵌套方式极不推荐,一般嵌套的层级最好不要超过一级

7.  scope路由
:path 改变Path,:module 改变Controller, :name_prefix || :as 改变  helper

  scope 'admin' do

     resources :posts

   end

  #行当于:

  scope :path => 'admin' do

        resources :posts

  end

 

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 'admin', :module => 'admin', :name_prefix => 'admin' do

        resources :posts

   end

  # 相当于: namespace 'admin' do resources :posts end

 

8.  在路由中定义跳转: 

   match "/posts/github" => redirect(http://github.com/rails.atom)

   match "/foo/:id", :to => redirect("/bar/%{id}s") # 地址 /foo/1 会自动跳转到 /bar/1s

   match 'account/proc/:name', :to => redirect {|params| "/#{params[:name].pluralize}" }  # /account/proc/inosin 会自动跳转到 /inosins

  match "/stories" => redirect {|p, req| "/posts/#{req.subdomain}" }

9. 路由中的限制:

   match "/posts/show/:id", :to => "posts#index", :id => /\d+/  # 限制 id 只能为数字

   match "/posts/show/:id", :to => "posts#index", :constraints => {:id => /\d+/}

   match "photos", :constraints => {:subdomain => "admin"} # 限制子域名

   constraints(:ip => /127.0.0.1/) do # 限制访问者 IP

      match '/questions', :to => redirect(http://www.stackoverflow.com/)

   end 

   match "/ttt" => proc{|env| [200, {}, ["hello test"]]}, \ :constraints => {:subdomain => "test", :ip => /192\.168\.1\.\d+/} # 当访问者 ip 是 192.168.1.* 的来访者访问 子域名为 "test"

10. 路由通配符:

   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"

11.  Rack:

   match "/foo", :to => proc {|env| [200, {}, ["Hello world"]] }

   match 'rocketeer.js' => ::TestRoutingMapper::RocketeerApp RocketeerApp = lambda { |env| [200, {"Content-Type" => "text/html"}, ["javascripts"]] }

posted on 2011-09-13 14:26  北国的雨  阅读(1589)  评论(1编辑  收藏  举报

导航