rails3 正则路由-----Rails3 Route用法集锦
来源:互联网 发布:淘宝banner全屏代码 编辑:程序博客网 时间:2024/06/07 02:53
rails3 正则路由
2010-10-16
Rails3 Route用法集锦
文章分类:Ruby编程默认路由:
正则路由:
命名路由:
根路由:
路由简写技巧:
:to 键的省略:
注意:
:as 在rails3中是改变 helper, 在rails2中是改变 path
当路径和控制器(及action)一至时,可省略指派控制器部分
Verb路由
当需要限制http请求方法的时候通过键 :via ,也可以直接把方法写在最前面:
resources路由:
:shallow用法:
Rails3中的shallow用法与Rails2中一致
使用: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选项的有效范围是对自身及嵌套的资源都有效,如下面这个例子:
这个例子中 albums、photos、images 都会使用简化方式,而 magazines 不会。特别注意:这种嵌套方式极不推荐,一般嵌套的层级最好不要超过一级
scope路由
:path 改变Path,:module 改变Controller, :name_prefix || :as 改变 helper
生成路由:
postsGET/admin/posts(.:format){:controller=>"posts", :action=>"index"}postsPOST/admin/posts(.:format){:controller=>"posts", :action=>"create"}new_postGET/admin/posts/new(.:format){:controller=>"posts", :action=>"new"}edit_postGET/admin/posts/:id/edit(.:format){:controller=>"posts", :action=>"edit"}postGET/admin/posts/:id(.:format){:controller=>"posts", :action=>"show"}postPUT/admin/posts/:id(.:format){:controller=>"posts", :action=>"update"}postDELETE/admin/posts/:id(.:format){:controller=>"posts", :action=>"destroy"}
生成路由:
postsGET/posts(.:format){:controller=>"admin/posts", :action=>"index"}postsPOST/posts(.:format){:controller=>"admin/posts", :action=>"create"}new_postGET/posts/new(.:format){:controller=>"admin/posts", :action=>"new"}edit_postGET/posts/:id/edit(.:format){:controller=>"admin/posts", :action=>"edit"}postGET/posts/:id(.:format){:controller=>"admin/posts", :action=>"show"}postPUT/posts/:id(.:format){:controller=>"admin/posts", :action=>"update"}postDELETE/posts/:id(.:format){:controller=>"admin/posts", :action=>"destroy"}
生成路由:
admin_postsGET/posts(.:format){:controller=>"posts", :action=>"index"}admin_postsPOST/posts(.:format){:controller=>"posts", :action=>"create"}new_admin_postGET/posts/new(.:format){:controller=>"posts", :action=>"new"}edit_admin_postGET/posts/:id/edit(.:format){:controller=>"posts", :action=>"edit"}admin_postGET/posts/:id(.:format){:controller=>"posts", :action=>"show"}admin_postPUT/posts/:id(.:format){:controller=>"posts", :action=>"update"}admin_postDELETE/posts/:id(.:format){:controller=>"posts", :action=>"destroy"}
生成路由:
admin_postsGET/admin/posts(.:format){:controller=>"admin/posts", :action=>"index"}admin_postsPOST/admin/posts(.:format){:controller=>"admin/posts", :action=>"create"}new_admin_postGET/admin/posts/new(.:format){:controller=>"admin/posts", :action=>"new"}edit_admin_postGET/admin/posts/:id/edit(.:format){:controller=>"admin/posts", :action=>"edit"}admin_postGET/admin/posts/:id(.:format){:controller=>"admin/posts", :action=>"show"}admin_postPUT/admin/posts/:id(.:format){:controller=>"admin/posts", :action=>"update"}admin_postDELETE/admin/posts/:id(.:format){:controller=>"admin/posts", :action=>"destroy"}
在路由中定义跳转:
路由中的限制:
路由通配符:
Rack:
参考文档:
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
- # Rails3:
- match '/:controller(/:action(/:id))'
- # Rails2:
- map.connect ':controller/:action/:id'
正则路由:
- # Rails3:
- match 'products/:id', :to => 'catalog#view'
- # Rails2:
- map.connect 'products/:id', :controller => 'catalog', :action => 'view'
命名路由:
- # Rails3:
- match 'logout', :to => 'sessions#destroy', :as => 'logout'
- # Rails2:
- map.logout 'logout', :controller => 'sessions', :action => ''
根路由:
- # Rails3:
- root => 'welcome#show'
- # Rails2:
- map.root :controller => 'welcome', :action => 'show'
路由简写技巧:
: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'
Verb路由
当需要限制http请求方法的时候通过键 :via ,也可以直接把方法写在最前面:
- get 'account/overview'
- # 相当于:
- match 'account/overview', :via => 'get'
- match 'account/setup', :via => [:get, :post]
- # 支持get\post\put\delete四种HTTP方法
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
:shallow用法:
Rails3中的shallow用法与Rails2中一致
- 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选项的有效范围是对自身及嵌套的资源都有效,如下面这个例子:
- 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
- scope 'admin' do
- resources :posts
- end
- # 行当于:
- scope :path => 'admin' do
- resources :posts
- end
生成路由:
postsGET/admin/posts(.:format){:controller=>"posts", :action=>"index"}postsPOST/admin/posts(.:format){:controller=>"posts", :action=>"create"}new_postGET/admin/posts/new(.:format){:controller=>"posts", :action=>"new"}edit_postGET/admin/posts/:id/edit(.:format){:controller=>"posts", :action=>"edit"}postGET/admin/posts/:id(.:format){:controller=>"posts", :action=>"show"}postPUT/admin/posts/:id(.:format){:controller=>"posts", :action=>"update"}postDELETE/admin/posts/:id(.:format){:controller=>"posts", :action=>"destroy"}
- scope :module => 'admin' do
- resources :posts
- end
- # 相当于:
- resources :posts, :module => 'admin'
生成路由:
postsGET/posts(.:format){:controller=>"admin/posts", :action=>"index"}postsPOST/posts(.:format){:controller=>"admin/posts", :action=>"create"}new_postGET/posts/new(.:format){:controller=>"admin/posts", :action=>"new"}edit_postGET/posts/:id/edit(.:format){:controller=>"admin/posts", :action=>"edit"}postGET/posts/:id(.:format){:controller=>"admin/posts", :action=>"show"}postPUT/posts/:id(.:format){:controller=>"admin/posts", :action=>"update"}postDELETE/posts/:id(.:format){:controller=>"admin/posts", :action=>"destroy"}
- scope :name_prefix => 'admin' do
- resources :posts
- end
- # 相当于:
- resources :posts, :name_prefix => 'admin'
生成路由:
admin_postsGET/posts(.:format){:controller=>"posts", :action=>"index"}admin_postsPOST/posts(.:format){:controller=>"posts", :action=>"create"}new_admin_postGET/posts/new(.:format){:controller=>"posts", :action=>"new"}edit_admin_postGET/posts/:id/edit(.:format){:controller=>"posts", :action=>"edit"}admin_postGET/posts/:id(.:format){:controller=>"posts", :action=>"show"}admin_postPUT/posts/:id(.:format){:controller=>"posts", :action=>"update"}admin_postDELETE/posts/:id(.:format){:controller=>"posts", :action=>"destroy"}
- scope 'admin', :module => 'admin', :name_prefix => 'admin' do
- resources :posts
- end
- # 相当于:
- namespace 'admin' do
- resources :posts
- end
生成路由:
admin_postsGET/admin/posts(.:format){:controller=>"admin/posts", :action=>"index"}admin_postsPOST/admin/posts(.:format){:controller=>"admin/posts", :action=>"create"}new_admin_postGET/admin/posts/new(.:format){:controller=>"admin/posts", :action=>"new"}edit_admin_postGET/admin/posts/:id/edit(.:format){:controller=>"admin/posts", :action=>"edit"}admin_postGET/admin/posts/:id(.:format){:controller=>"admin/posts", :action=>"show"}admin_postPUT/admin/posts/:id(.:format){:controller=>"admin/posts", :action=>"update"}admin_postDELETE/admin/posts/:id(.:format){:controller=>"admin/posts", :action=>"destroy"}
在路由中定义跳转:
- 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}" }
路由中的限制:
- # 限制 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+/}
路由通配符:
- 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:
- match "/foo", :to => proc {|env| [200, {}, ["Hello world"]] }
- match 'rocketeer.js' => ::TestRoutingMapper::RocketeerApp
- RocketeerApp = lambda { |env|
- [200, {"Content-Type" => "text/html"}, ["javascripts"]]
- }
参考文档:
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
- rails3 正则路由-----Rails3 Route用法集锦
- Rails3 Route用法集锦
- Rails3 Route 用法集锦
- Rails3 Route 用法集锦
- rails3下分离route配置文件
- rails3 数据格式
- Rails3教程系列之一:Rails3入门
- Rails3.1.1到Rails3.2.2升级笔记
- Rails3世界系列报道
- rvm+ruby1.9.2+rails3
- Ubuntu 10.04 Rails3
- rails3项目架构解析
- rails3 guides生成
- Rails3 入门 前言
- rails3 mysql2安装
- Rails3 session应用
- Rails3 Ajax 筆記
- ubuntu intall rails3
- Hibernate中复合主键的配置
- NYOJ——114题某种序列
- Android利用NDK实现C++开发
- sql获取自增行号
- 在数据库中条件查询很慢的时候,如何优化
- rails3 正则路由-----Rails3 Route用法集锦
- linux内核分析之系统调用
- oracle中多实例配置多监听器的问题
- Hibernate中get方法和load方法的区别
- hdoj 2046 骨牌铺方格(递推)
- Win环境安装VMware Server 2.0手记
- MyEclipse 自动提示配置
- C#关于textbox的text属性
- 2011年终总结