Ruby on Rails,Routes配置routes.rb及请求解析规则
http://blog.csdn.net/abbuggy/article/details/7516224
在《Ruby on Rails,服务端如何响应页面提交的请求》中我们知道,页面提交给web服务器的请求先是尝试直接查找并返回public目录中的资源,如果没有找到则将请求交给Rails的Routes。Routes根据路由配置,将请求转化为对Controller中的Action并调用之。Routes的配置可说的细节很多,最开始我们先关注三种最简单的方式:Simple route,Default route,Root route。Routes的配置信息存放在config/routes.rb文件当中。
当前我的routes.rb文件如下所示,大段被注释掉的内容是各种配置用法的举例先不用理会,其中第二行get "demo/index"这句就是在《Ruby on Rails,创建最简单的视图/控制器单元Hello World》中通过调用生成视图/控制器命令被自动添加过来的。这就是一个Simple route。
在Routes配置文件中,越靠上的一行配置拥有越高的优先级。
SimpleSite::Application.routes.draw do get "demo/index" # The priority is based upon order of creation: # first created -> highest priority. # Sample of regular route: # match 'products/:id' => 'catalog#view' # Keep in mind you can assign values other than :controller and :action # Sample of named route: # match 'products/:id/purchase' => 'catalog#purchase', :as => :purchase # This route can be invoked with purchase_url(:id => product.id) # Sample resource route (maps HTTP verbs to controller actions automatically): # resources :products # Sample resource route with options: # resources :products do # member do # get 'short' # post 'toggle' # end # # collection do # get 'sold' # end # end # Sample resource route with sub-resources: # resources :products do # resources :comments, :sales # resource :seller # end # Sample resource route with more complex sub-resources # resources :products do # resources :comments # resources :sales do # get 'recent', :on => :collection # end # end # Sample resource route within a namespace: # namespace :admin do # # Directs /admin/products/* to Admin::ProductsController # # (app/controllers/admin/products_controller.rb) # resources :products # end # You can have the root of your site routed with "root" # just remember to delete public/index.html. # root :to => 'welcome#index' # See how all your routes lay out with "rake routes" # This is a legacy wild controller route that's not recommended for RESTful applications. # Note: This route will make all actions in every controller accessible via GET requests. # match ':controller(/:action(/:id))(.:format)' endget "demo/index"的意思是将demo/index的请求调用名为demo的Controller中名为index的Action。
match "demo/index", :to => "demo#index"
这种路由配置最简单易懂但是比较不易于维护,因为我们要在每次增加视图/控制器时维护Routes配置。我们想使用一个更加通用的规则,在这个规则的帮助下我们修改视图/控制器规则后依然能够工作-Default route。
get /:controller/:action/:id的意思是调用名为demo的Controller中名为index的Action并传入id值。
match ':controller(/:action(/:id(.:format)))'routes.rb文件的最下面也就是优先级最低的一条配置既是Default route规则。如果传入的请求没有能够匹配Routes配置中的任何一条规则,则将按照这一条规则对请求进行解析。我们将这条规则前的注释符号删除,以便启用这条规则,同时也就不需要文件顶部的那条get "demo/index"。
SimpleSite::Application.routes.draw do match ':controller(/:action(/:id))(.:format)' end
修改了routes.rb之后需要重启服务器。请求“localhost:3000/demo/index”依然能够转向之前的页面,说明Default route配置包含了之前Simple route的情况。
由于我们之前删除了public目录中的index.html,所以当以“localhost:3000/”请求时将没有规则能够匹配“/”。
这时,我们需要配置Root route。
root :to => "demo#index"此时route.rb如下
SimpleSite::Application.routes.draw do root :to => "demo#index" match ':controller(/:action(/:id))(.:format)' end
重启服务器后访问“localhost:3000/”得到了配置中规定的页面。
http://blog.csdn.net/abbuggy/article/details/7516224