SlideShare una empresa de Scribd logo
1 de 17
Descargar para leer sin conexión
Rails 3 中的路由
zhangyuan
13年5月9日星期四
The Purpose of the
Rails Router
• Connecting URLs to Code
• Generating Paths and URLs from Code
13年5月9日星期四
演示
• rake routes
• app.posts_url
• Rails.application.routes.recognize_path('/posts/new')
• 必须已经创建好必要的Controller和Action
• Rails.application.routes.url_helpers.new_post_path
13年5月9日星期四
分类
• Resource Routing
• Non-Resourceful Routes
13年5月9日星期四
资源路由
• resources & resource
• :ony & :except
• :path
• :path_names
• member & collection
13年5月9日星期四
方法
• namespace
• scope
• :as
• :module
• ....
• ...
13年5月9日星期四
通用选项
• :as
• :defaults
• :subdomain
• :constraints
• etc.
13年5月9日星期四
子域名
• 完成路由的两个目标
13年5月9日星期四
配置子域名
scope subdomain: 'admin' do
resources :posts
end
scope constraints: {subdomain: 'admin'} do
resources :posts
end
scope subdomain: 'admin', constraints: {subdomain: 'admin'} do
resources :posts
end
13年5月9日星期四
配置子域名
scope subdomain: 'admin' do
resources :posts
end
> app.posts_url
=> "http://www.example.com/posts"
> Rails.application.routes.recognize_path("http://admin.example.com/posts")
=> {:action=>"index", :controller=>"posts"}
> Rails.application.routes.recognize_path("http://example.com/posts")
ActionController::RoutingError: No route matches "http://example.com/posts"
> Rails.application.routes.recognize_path("http://somesubdomain.example.com/posts")
ActionController::RoutingError: No route matches "http://somesubdomain.example.com/posts"
13年5月9日星期四
配置子域名
scope constraints: {subdomain: 'admin'} do
resources :posts
end
> app.posts_url
=> "http://admin.example.com/posts"
> Rails.application.routes.recognize_path("http://example.com/posts")
=> {:subdomain=>"admin", :action=>"index", :controller=>"posts"}
> Rails.application.routes.recognize_path("http://www.example.com/posts")
=> {:subdomain=>"admin", :action=>"index", :controller=>"posts"}
> Rails.application.routes.recognize_path("http://admin.example.com/posts")
=> {:subdomain=>"admin", :action=>"index", :controller=>"posts"}
> Rails.application.routes.recognize_path("http://somesubdomain.example.com/posts")
=> {:subdomain=>"admin", :action=>"index", :controller=>"posts"}
13年5月9日星期四
配置子域名
scope subdomain: 'admin', constraints: {subdomain: 'admin'} do
resources :posts
end
app.posts_url
=> "http://admin.example.com/posts"
> Rails.application.routes.recognize_path("http://admin.example.com/posts")
=> {:subdomain=>"admin", :action=>"index", :controller=>"posts"}
> Rails.application.routes.recognize_path("http://example.com/posts")
ActionController::RoutingError: No route matches "http://example.com/posts"
> Rails.application.routes.recognize_path("http://www.example.com/posts")
ActionController::RoutingError: No route matches "http://www.example.com/posts"
13年5月9日星期四
配置子域名
scope subdomain: 'admin', constraints: {subdomain: /^(admin|
manage)$/ } do
resources :posts
end
app.posts_url
=> "http://admin.example.com/posts"
> Rails.application.routes.recognize_path("http://admin.example.com/posts")
=> {:subdomain=>"admin", :action=>"index", :controller=>"posts"}
> Rails.application.routes.recognize_path("http://manage.example.com/posts")
=> {:subdomain=>"admin", :action=>"index", :controller=>"posts"}
> Rails.application.routes.recognize_path("http://example.com/posts")
ActionController::RoutingError: No route matches "http://example.com/posts"
> Rails.application.routes.recognize_path("http://www.example.com/posts")
ActionController::RoutingError: No route matches "http://www.example.com/posts"
13年5月9日星期四
redirect
• 默认为301跳转
13年5月9日星期四
Rails Engine
• mount
13年5月9日星期四
Rspec测试
• rspec/routes/
• spec/routes/posts_routing_spec.rb
13年5月9日星期四
参考资料
• http://guides.rubyonrails.org/routing.html
13年5月9日星期四

Más contenido relacionado

Destacado

PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...DevGAMM Conference
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationErica Santiago
 

Destacado (20)

PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
 

Rails 3中路由

  • 2. The Purpose of the Rails Router • Connecting URLs to Code • Generating Paths and URLs from Code 13年5月9日星期四
  • 3. 演示 • rake routes • app.posts_url • Rails.application.routes.recognize_path('/posts/new') • 必须已经创建好必要的Controller和Action • Rails.application.routes.url_helpers.new_post_path 13年5月9日星期四
  • 4. 分类 • Resource Routing • Non-Resourceful Routes 13年5月9日星期四
  • 5. 资源路由 • resources & resource • :ony & :except • :path • :path_names • member & collection 13年5月9日星期四
  • 6. 方法 • namespace • scope • :as • :module • .... • ... 13年5月9日星期四
  • 7. 通用选项 • :as • :defaults • :subdomain • :constraints • etc. 13年5月9日星期四
  • 9. 配置子域名 scope subdomain: 'admin' do resources :posts end scope constraints: {subdomain: 'admin'} do resources :posts end scope subdomain: 'admin', constraints: {subdomain: 'admin'} do resources :posts end 13年5月9日星期四
  • 10. 配置子域名 scope subdomain: 'admin' do resources :posts end > app.posts_url => "http://www.example.com/posts" > Rails.application.routes.recognize_path("http://admin.example.com/posts") => {:action=>"index", :controller=>"posts"} > Rails.application.routes.recognize_path("http://example.com/posts") ActionController::RoutingError: No route matches "http://example.com/posts" > Rails.application.routes.recognize_path("http://somesubdomain.example.com/posts") ActionController::RoutingError: No route matches "http://somesubdomain.example.com/posts" 13年5月9日星期四
  • 11. 配置子域名 scope constraints: {subdomain: 'admin'} do resources :posts end > app.posts_url => "http://admin.example.com/posts" > Rails.application.routes.recognize_path("http://example.com/posts") => {:subdomain=>"admin", :action=>"index", :controller=>"posts"} > Rails.application.routes.recognize_path("http://www.example.com/posts") => {:subdomain=>"admin", :action=>"index", :controller=>"posts"} > Rails.application.routes.recognize_path("http://admin.example.com/posts") => {:subdomain=>"admin", :action=>"index", :controller=>"posts"} > Rails.application.routes.recognize_path("http://somesubdomain.example.com/posts") => {:subdomain=>"admin", :action=>"index", :controller=>"posts"} 13年5月9日星期四
  • 12. 配置子域名 scope subdomain: 'admin', constraints: {subdomain: 'admin'} do resources :posts end app.posts_url => "http://admin.example.com/posts" > Rails.application.routes.recognize_path("http://admin.example.com/posts") => {:subdomain=>"admin", :action=>"index", :controller=>"posts"} > Rails.application.routes.recognize_path("http://example.com/posts") ActionController::RoutingError: No route matches "http://example.com/posts" > Rails.application.routes.recognize_path("http://www.example.com/posts") ActionController::RoutingError: No route matches "http://www.example.com/posts" 13年5月9日星期四
  • 13. 配置子域名 scope subdomain: 'admin', constraints: {subdomain: /^(admin| manage)$/ } do resources :posts end app.posts_url => "http://admin.example.com/posts" > Rails.application.routes.recognize_path("http://admin.example.com/posts") => {:subdomain=>"admin", :action=>"index", :controller=>"posts"} > Rails.application.routes.recognize_path("http://manage.example.com/posts") => {:subdomain=>"admin", :action=>"index", :controller=>"posts"} > Rails.application.routes.recognize_path("http://example.com/posts") ActionController::RoutingError: No route matches "http://example.com/posts" > Rails.application.routes.recognize_path("http://www.example.com/posts") ActionController::RoutingError: No route matches "http://www.example.com/posts" 13年5月9日星期四