Rails3 Route用法集锦

默认路由: 
Ruby代码   收藏代码
  1. # Rails3:  
  2. match '/:controller(/:action(/:id))'  
  3. # Rails2:  
  4. map.connect ':controller/:action/:id'  


正则路由: 
Ruby代码   收藏代码
  1. # Rails3:  
  2. match 'products/:id':to => 'catalog#view'  
  3. # Rails2:  
  4. map.connect 'products/:id':controller => 'catalog':action => 'view'  


命名路由: 
Ruby代码   收藏代码
  1. # Rails3:  
  2. match 'logout':to => 'sessions#destroy':as => 'logout'  
  3. # Rails2:  
  4. map.logout 'logout':controller => 'sessions':action => ''  


根路由: 
Ruby代码   收藏代码
  1. # Rails3:  
  2. root :to => 'welcome#show'  
  3. # Rails2:  
  4. map.root :controller => 'welcome':action => 'show'  


路由简写技巧: 
:to 键的省略: 
Ruby代码   收藏代码
  1. match 'account' => 'account#index'  
  2. # 相当于:  
  3. match 'account':to => 'account#index'  
  4.   
  5. match 'info' => 'projects#info':as => 'info'  

注意: 
:as 在rails3中是改变 helper, 在rails2中是改变 path
 

当路径和控制器(及action)一至时,可省略指派控制器部分 
Ruby代码   收藏代码
  1. match 'account/overview'  
  2. # 相当于:  
  3. match 'account/overview':to => 'account#overview'  


Verb路由 
当需要限制http请求方法的时候通过键  :via ,也可以直接把方法写在最前面: 
Ruby代码   收藏代码
  1. get 'account/overview'  
  2. # 相当于:  
  3. match 'account/overview':via => 'get'  
  4.   
  5. match 'account/setup':via => [:get:post]  
  6. # 支持get\post\put\delete四种HTTP方法  


resources路由: 
Ruby代码   收藏代码
  1. resources :posts:except => [:index]  
  2. resources :posts:only => [:new:create]  
  3.   
  4. # edit_post GET    /posts/:id/modify(.:format) {:controller=>"posts", :action=>"edit"}  
  5. resources :posts:path_names => { :edit => 'modify' }   
  6.   
  7. resources :projects do  
  8.   resources :tasks:people  
  9. end  
  10.   
  11. resources :products do  
  12.   collection do  
  13.     get  :sold  
  14.     post :on_offer:search  
  15.   end  
  16.   get :buy:on => :member  
  17.   post :batch:on => :collection  
  18. end  
  19.   
  20. resource :session do  
  21.   get :create  
  22. end  


:shallow用法: 
Rails3中的shallow用法与Rails2中一致 
Ruby代码   收藏代码
  1. resources :blogs:shallow => true do  
  2.   resources :comments  
  3. 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选项的有效范围是对自身及嵌套的资源都有效,如下面这个例子: 
Ruby代码   收藏代码
  1. resources :publishers do  
  2.   resources :magazines do  
  3.     resources :albums:shallow => true do  
  4.       resources :photos do  
  5.         resources :images  
  6.       end  
  7.     end  
  8.   end  
  9. end  

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

scope路由 
:path 改变Path,:module 改变Controller, :name_prefix || :as 改变  helper 
Ruby代码   收藏代码
  1. scope 'admin' do  
  2.   resources :posts  
  3. end  
  4. # 行当于:  
  5. scope :path => 'admin' do  
  6.   resources :posts  
  7. end  

生成路由: 
posts GET /admin/posts(.:format) {:controller=>"posts", :action=>"index"}
posts POST /admin/posts(.:format) {:controller=>"posts", :action=>"create"}
new_post GET /admin/posts/new(.:format) {:controller=>"posts", :action=>"new"}
edit_post GET /admin/posts/:id/edit(.:format) {:controller=>"posts", :action=>"edit"}
post GET /admin/posts/:id(.:format) {:controller=>"posts", :action=>"show"}
post PUT /admin/posts/:id(.:format) {:controller=>"posts", :action=>"update"}
post DELETE /admin/posts/:id(.:format) {:controller=>"posts", :action=>"destroy"}

Ruby代码   收藏代码
  1. scope :module => 'admin' do  
  2.   resources :posts  
  3. end  
  4. # 相当于:  
  5. resources :posts:module => 'admin'  

生成路由: 
posts GET /posts(.:format) {:controller=>"admin/posts", :action=>"index"}
posts POST /posts(.:format) {:controller=>"admin/posts", :action=>"create"}
new_post GET /posts/new(.:format) {:controller=>"admin/posts", :action=>"new"}
edit_post GET /posts/:id/edit(.:format) {:controller=>"admin/posts", :action=>"edit"}
post GET /posts/:id(.:format) {:controller=>"admin/posts", :action=>"show"}
post PUT /posts/:id(.:format) {:controller=>"admin/posts", :action=>"update"}
post DELETE /posts/:id(.:format) {:controller=>"admin/posts", :action=>"destroy"}

Ruby代码   收藏代码
  1. scope :name_prefix => 'admin' do  
  2.   resources :posts  
  3. end  
  4. # 相当于:  
  5. resources :posts:name_prefix => 'admin'  

生成路由: 
admin_posts GET /posts(.:format) {:controller=>"posts", :action=>"index"}
admin_posts POST /posts(.:format) {:controller=>"posts", :action=>"create"}
new_admin_post GET /posts/new(.:format) {:controller=>"posts", :action=>"new"}
edit_admin_post GET /posts/:id/edit(.:format) {:controller=>"posts", :action=>"edit"}
admin_post GET /posts/:id(.:format) {:controller=>"posts", :action=>"show"}
admin_post PUT /posts/:id(.:format) {:controller=>"posts", :action=>"update"}
admin_post DELETE /posts/:id(.:format) {:controller=>"posts", :action=>"destroy"}

Ruby代码   收藏代码
  1. scope 'admin':module => 'admin':name_prefix => 'admin' do  
  2.   resources :posts  
  3. end  
  4. # 相当于:  
  5. namespace 'admin' do   
  6.   resources :posts  
  7. end  

生成路由: 
admin_posts GET /admin/posts(.:format) {:controller=>"admin/posts", :action=>"index"}
admin_posts POST /admin/posts(.:format) {:controller=>"admin/posts", :action=>"create"}
new_admin_post GET /admin/posts/new(.:format) {:controller=>"admin/posts", :action=>"new"}
edit_admin_post GET /admin/posts/:id/edit(.:format) {:controller=>"admin/posts", :action=>"edit"}
admin_post GET /admin/posts/:id(.:format) {:controller=>"admin/posts", :action=>"show"}
admin_post PUT /admin/posts/:id(.:format) {:controller=>"admin/posts", :action=>"update"}
admin_post DELETE /admin/posts/:id(.:format) {:controller=>"admin/posts", :action=>"destroy"}


在路由中定义跳转: 
Ruby代码   收藏代码
  1. match "/posts/github" => redirect("http://github.com/rails.atom")  
  2.   
  3. # 地址 /foo/1 会自动跳转到 /bar/1s  
  4. match "/foo/:id":to => redirect("/bar/%{id}s")    
  5.   
  6. # /account/proc/inosin 会自动跳转到 /inosins  
  7. match 'account/proc/:name':to => redirect {|params|   
  8. "/#{params[:name].pluralize}" }  
  9.   
  10. match "/stories" => redirect {|p, req| "/posts/#{req.subdomain}" }   


路由中的限制: 
Ruby代码   收藏代码
  1. # 限制 id 只能为数字  
  2. match "/posts/show/:id":to => "posts#index":id => /\d+/  
  3. match "/posts/show/:id":to => "posts#index":constraints => {:id => /\d+/}  
  4.   
  5. # 限制子域名  
  6. match "photos":constraints => {:subdomain => "admin"}   
  7.   
  8. # 限制访问者 IP  
  9. constraints(:ip => /127.0.0.1/) do  
  10.   match  '/questions':to => redirect("http://www.stackoverflow.com/")  
  11. end  
  12.   
  13. # 当访问者 ip 是 192.168.1.* 的来访者访问 子域名为 "test"  
  14. match "/ttt" => proc{|env| [200, {}, ["hello test"]]}, \  
  15.     :constraints => {:subdomain => "test":ip => /192\.168\.1\.\d+/}   


路由通配符: 
Ruby代码   收藏代码
  1. resources :photos:id => /\d+/  
  2. match 'photos/*other' => 'photos#unknown'  
  3. #上面这两行路由则会把不符合7种path的其他url全部解析到PhotoController#unknown中去处理,params[:other]可得到path中/photos/之后的部分,注意这两行的顺序不能颠倒  
  4.   
  5. match 'books/*section/:title' => 'books#show'   
  6. # 例如:books/some/section/last-words-a-memoir 中 params[:section] = "some/section", params[:title] = "last-words-a-memoir".  
  7.   
  8. match '*a/foo/*b' => 'test#index'   
  9. # 例如:zoo/woo/foo/bar/baz 中 params[:a] = "zoo/woo", params[:b] = "bar/baz"  


Rack: 
Ruby代码   收藏代码
  1. match "/foo":to => proc {|env| [200, {}, ["Hello world"]] }  
  2.    
  3. match 'rocketeer.js' => ::TestRoutingMapper::RocketeerApp  
  4.    
  5. RocketeerApp = lambda { |env|  
  6.   [200, {"Content-Type" => "text/html"}, ["javascripts"]]  
  7. }  


参考文档: 
http://www.engineyard.com/blog/2010/the-lowdown-on-routes-in-rails-3/ 
http://www.railsdispatch.com/posts/rails-routing 
http://guides.rails.info/routing.html 
http://asciicasts.com/episodes/203-routing-in-rails-3 
http://asciicasts.com/episodes/231-routing-walkthrough 
http://asciicasts.com/episodes/232-routing-walkthrough-part-2

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。、可私 6信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。、可 6私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 、4下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。、可私 6信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值