Ruby On Rails的路由如何知道当我们点击url时要点击哪个控制器

时间:2022-04-25 06:30:46

How ROR routes recognise which controller method to hit while we hit an url.

当我们点击一个url时,ROR路由如何识别要击中的控制器方法。

2 个解决方案

#1


4  

It's a way to redirect incoming requests to controllers and actions. It replaces the mod_rewrite rules. Best of all, Rails' Routing works with any web server. Routes are defined in app/config/routes.rb.

它是一种将传入请求重定向到控制器和操作的方法。它替换了mod_rewrite规则。最重要的是,Rails的路由适用于任何web服务器。路由是在app/config/ routing .rb中定义的。

Think of creating routes as drawing a map for your requests. The map tells them where to go based on some predefined pattern.

考虑创建路由,以绘制请求的映射。地图根据预定义的模式告诉他们去哪里。

The routes.rb file defines the actions available in the applications and the type of action such as get, post, and patch.

的路线。rb文件定义了应用程序中可用的操作以及操作类型,如get、post和patch。

like: get 'photos/:id' => 'photos#show', :defaults => { :format => 'jpg' }

例如:获取'photos/:id' => 'photos#show',:default => {:format => 'jpg'}

the normalise value of above route is.

上述路径的正态值为。

app: #<ActionDispatch::Routing::RouteSet::Dispatcher:0x007fd05e0cf7e8
           @defaults={:format=>"jpg", :controller=>"photos", :action=>"show"},
           @glob_param=nil,
           @controller_class_names=#<ThreadSafe::Cache:0x007fd05e0cf7c0
           @backend={},
           @default_proc=nil>>
conditions: {:path_info=>"/photos/:id(.:format)", :required_defaults=>[:controller, :action], :request_method=>["GET"]}
requirements: {}
defaults: {:format=>"jpg", :controller=>"photos", :action=>"show"}
as: nil
anchor: true 

#2


3  

The official Ruby on Rails documentation explains this question in a thorough and understandable way:

Ruby on Rails官方文档以一种完全的、可理解的方式解释了这个问题:

http://guides.rubyonrails.org/routing.html

http://guides.rubyonrails.org/routing.html

#1


4  

It's a way to redirect incoming requests to controllers and actions. It replaces the mod_rewrite rules. Best of all, Rails' Routing works with any web server. Routes are defined in app/config/routes.rb.

它是一种将传入请求重定向到控制器和操作的方法。它替换了mod_rewrite规则。最重要的是,Rails的路由适用于任何web服务器。路由是在app/config/ routing .rb中定义的。

Think of creating routes as drawing a map for your requests. The map tells them where to go based on some predefined pattern.

考虑创建路由,以绘制请求的映射。地图根据预定义的模式告诉他们去哪里。

The routes.rb file defines the actions available in the applications and the type of action such as get, post, and patch.

的路线。rb文件定义了应用程序中可用的操作以及操作类型,如get、post和patch。

like: get 'photos/:id' => 'photos#show', :defaults => { :format => 'jpg' }

例如:获取'photos/:id' => 'photos#show',:default => {:format => 'jpg'}

the normalise value of above route is.

上述路径的正态值为。

app: #<ActionDispatch::Routing::RouteSet::Dispatcher:0x007fd05e0cf7e8
           @defaults={:format=>"jpg", :controller=>"photos", :action=>"show"},
           @glob_param=nil,
           @controller_class_names=#<ThreadSafe::Cache:0x007fd05e0cf7c0
           @backend={},
           @default_proc=nil>>
conditions: {:path_info=>"/photos/:id(.:format)", :required_defaults=>[:controller, :action], :request_method=>["GET"]}
requirements: {}
defaults: {:format=>"jpg", :controller=>"photos", :action=>"show"}
as: nil
anchor: true 

#2


3  

The official Ruby on Rails documentation explains this question in a thorough and understandable way:

Ruby on Rails官方文档以一种完全的、可理解的方式解释了这个问题:

http://guides.rubyonrails.org/routing.html

http://guides.rubyonrails.org/routing.html