久久精品精选,精品九九视频,www久久只有这里有精品,亚洲熟女乱色综合一区
    分享

    Routing: Native Ruby Rewriting

     沁平 2008-03-30

    1. An Introduction to Routes

    This patch implements native Ruby rewriting for Rails, thus shifting the responsibility of URL parsing from the webserver to Rails itself. This has been a requested feature for two primary reasons.

    1. Not all webservers support rewriting. By moving this code to the framework Rails is able to function “out of the box” on almost all webservers.
    1. A rewriting implementation in Rails can also be used to generate custom URLs by linking it to the standard URL helpers such as url_for, link_to, and redirect_to.

    Routes have been designed to be fully customizable by the average Ruby programmer. You’ll no longer have to be a htaccess wizard to have pretty URLs for your site.

    Routes are defined in config/routes.rb. This file is a typical ruby source file which contains a document similar to

    ActionController::Routing.draw do |map|
    map.connect ‘:controller/:action/:id‘
    end
    

    Instead of wading through the tedious and obvious, let’s dive right in.

    Example 1—Setting A Default Controller

    Let’s say you’re one of those cool kids with a blog, and you’d like http://www./ to show your recent posts.

    Case 1—BlogController#index

    Lets say your BlogController’s index action is already setup to show the list of recent posts. To specify the default controller, we add :controller => ‘blog’ to the default route:

    ActionController::Routing.draw do |map|
    map.connect ‘:controller/:action/:id‘, :controller => ‘blog‘
    end
    

    As an additional perk, Routes will opt for the shortest path possible, so url_for :controller => ‘blog’, :action => ‘index’ will generate a url such as http://www./.

    Case 2—BlogController#recent

    Lets suppose that for whatever reason, your BlogController displays the recent posts with another action. We need to add a new Route that matches an empty path and points to BlogController’s recent action.

    ActionController::Routing.draw do |map|
    map.connect ‘‘, :controller => ‘blog‘, :action => ‘recent‘
    map.connect ‘:controller/:action/:id‘
    end
    

    Notice that we put our new route before the default Rails route. Routes are recognized and generated in the order they are defined. By placing our custom route before the default Rails route, we can be assured that url_for :controller => ‘blog’, :action => ‘recent’ will generate a URL with an empty path, such as http://www./.

    Note that if public/index.html exists, it will be served and Rails will not see the request. Be sure to delete the Congratulations page that ships with Rails.

    Example 2—Setting up date-based URLs

    Suppose you want to add a feature to your blog that let’s your visitors view posts by date. You’d like to allow them to browse by year, month, and day, and you already have an action which uses query or post parameters named year, month, and action.

    The ideal URL for this looks like http://www./2005/02/14, with the month and day components being optional. Because you’re a sane person, you decide that the date order should be YYYY/MM/DD, rather than some arcane order such as MM/DD/YYYY.

    Once again, we will want to add our custom route before the default Rails route. A first try at this might yield
      map.connect ‘:year/:month/:day‘, :controller => ‘blog‘, :action => ‘by_date‘
    
    Although this will successfully match http://www./date/2005/02/14, it will fail to do so for tt>http://www./date/2005/02—we need to mark the :month and :day components as optional. To do so, we add :month => nil to our route:
      map.connect ‘date/:year/:month/:day‘, :controller => ‘blog‘, :action => ‘by_date‘,
    :month => nil, :day => nil
    

    Now our custom route will recognize URLs such as http://www./date/2005/02 or even http://www./date/2005. On the flip side, our custom route will also be used automatically when URLs are generated with the standard forms helpers such as link_to and url_for.

    Now we run into another problem—our new route is too general, and it will catch all three-component URLs—such as posts/show/10. To combat this, we must tell place some requirements on our path components. Requirements are placed using a :requirements sub-hash like so:

      map.connect ‘date/:year/:month/:day‘, :controller => ‘blog‘, :action => ‘by_date‘,
    :month => nil, :day => nil,
    :requirements => {:year => /\d{4}/, :day => /\d{1,2}/, :month => /\d{1,2}/}
    

    Example 3—Posts by Category

    Lets move back to your BlogController. Suppose each of your posts belong to a category, and that we would like URLs to show items based on category, such as http://www./posts/rails. Lets assume that you also use the category “all” to mean show all categories. A quick route for this would be:

      map.connect ‘posts/:category‘, :controller => ‘blog‘, :action => ‘posts‘
    
    The problem with this route is that your link_to statements will all have to include :category => ‘all’. It would be nicer if our templates could contain
    <%= link_to ‘Posts‘, :controller => ‘blog‘, :action => ‘posts‘ %>

    We can achieve this by giving the route a default value for :category:

      map.connect ‘posts/:category‘, :controller => ‘blog‘, :action => ‘posts‘, :category => ‘all‘
    

    An added bonus is that the URL generated for the above link_to will omit the extraneous ‘all’ component, and be displayed as http://www./posts.

      本站是提供個人知識管理的網絡存儲空間,所有內容均由用戶發布,不代表本站觀點。請注意甄別內容中的聯系方式、誘導購買等信息,謹防詐騙。如發現有害或侵權內容,請點擊一鍵舉報。
      轉藏 分享 獻花(0

      0條評論

      發表

      請遵守用戶 評論公約

      類似文章 更多

      主站蜘蛛池模板: 国产精品免费中文字幕| 国产成人高清在线观看视频| 久久精品国产亚洲av麻豆软件| 欧美一进一出抽搐大尺度视频| 久久综合精品国产二区无码| 无翼乌工口肉肉无遮挡无码18 | 无码国产偷倩在线播放| 少妇粗大进出白浆嘿嘿视频| 国产永久免费高清在线观看| 色综合 图片区 小说区| 少妇人妻偷人精品视蜜桃| 日日婷婷夜日日天干| 国产老妇伦国产熟女老妇高清| 精品久久人人做爽综合| 唐人社视频呦一区二区| 国产成人精品无码免费看| 在线精品国产一区二区三区| 无码囯产精品一区二区免费 | 亚洲高清成人av在线| 丁香婷婷色综合激情五月| 日韩放荡少妇无码视频| 国产成人无码区免费内射一片色欲| 成人H视频在线观看| 国内精品久久久久久久影视| 成人亚欧欧美激情在线观看| 公喝错春药让我高潮| 小妖精又紧又湿高潮H视频69| 国产成人av电影在线观看第一页| 日韩av片无码一区二区不卡| 伊人狠狠色丁香婷婷综合| 一本一道VS无码中文字幕| 最新亚洲人成无码WWW| 国产仑乱无码内谢| 国产在线观看免费人成视频| 无码AV无码天堂资源网影音先锋| 99久久精品国产综合一区| 日韩大片高清播放器大全| 久久99精品久久久久久9| 国产农村妇女高潮大叫| 国产精品一码二码三码| 一本一道VS无码中文字幕|