7ur*_*m3n 6 ruby routing routes ruby-on-rails ruby-on-rails-4
我想了解更多有关Rails路由的信息.
会员和收藏
# Example resource route with options:
resources :products do
member do
get 'short'
post 'toggle'
end
collection do
get 'sold'
end
end
Run Code Online (Sandbox Code Playgroud)
命名空间和范围
# Example resource route within a namespace:
namespace :admin do
resources :products
end
scope :admin do
resources :products
end
Run Code Online (Sandbox Code Playgroud)
约束,Redirect_to
# Example resource route with options:
get "/questions", to: redirect {|params, req|
begin
id = req.params[:category_id]
cat = Category.find(id)
"/abc/#{cat.slug}"
rescue
"/questions"
end
}
Run Code Online (Sandbox Code Playgroud)
定制:
resources :profiles
Run Code Online (Sandbox Code Playgroud)
来自resource profiles编辑的原始网址.
http://localhost:3000/profiles/1/edit
Run Code Online (Sandbox Code Playgroud)
我想为只有点击的用户提供它,edit profile并在下面看到url.
http://localhost:3000/profile/edit
Run Code Online (Sandbox Code Playgroud)
此外,是否有先进的路由,大多数大公司如何设计铁路路线?如果有的话,我会很高兴看到新的路线.
谢谢 !
Muh*_*Ali 21
**Collection & Member routes**
Run Code Online (Sandbox Code Playgroud)
成员路由需要ID,因为它作用于成员.
收集路由不需要ID,因为它作用于对象集合
:member 用模式创建路径 /:controller/:id/:your_method
:collection 用模式创建路径 /:controller/:your_method
For example :
map.resources :users, :collection => { :abc => :get } => /users/abc
map.resources :users, :member => { :abc => :get } => /users/1/abc
**Scopes & Namespaces routes**
Run Code Online (Sandbox Code Playgroud)
namespace并且scope在Rails中routes影响控制器名称,URI和命名路由.范围方法为您提供细粒度控制:
scope 'url_path_prefix', module: 'module_prefix', as: 'named_route_prefix' do
resources :model_name
end
Run Code Online (Sandbox Code Playgroud)
For Example :
scope 'foo', module: 'bar', as: 'baz' do
resources :posts
end
Run Code Online (Sandbox Code Playgroud)
生产路线为:
Prefix Verb URI Pattern Controller#Action
baz_posts GET /foo/posts(.:format) bar/posts#index
POST /foo/posts(.:format) bar/posts#create
new_baz_post GET /foo/posts/new(.:format) bar/posts#new
edit_baz_post GET /foo/posts/:id/edit(.:format) bar/posts#edit
baz_post GET /foo/posts/:id(.:format) bar/posts#show
PATCH /foo/posts/:id(.:format) bar/posts#update
PUT /foo/posts/:id(.:format) bar/posts#update
DELETE /foo/posts/:id(.:format) bar/posts#destroy
Run Code Online (Sandbox Code Playgroud)
命名空间方法是一个简单的例子 - 它为所有内容添加前缀.
namespace :foo do
resources :posts
end
Run Code Online (Sandbox Code Playgroud)
生产路线为:
Prefix Verb URI Pattern Controller#Action
foo_posts GET /foo/posts(.:format) foo/posts#index
POST /foo/posts(.:format) foo/posts#create
new_foo_post GET /foo/posts/new(.:format) foo/posts#new
edit_foo_post GET /foo/posts/:id/edit(.:format) foo/posts#edit
foo_post GET /foo/posts/:id(.:format) foo/posts#show
PATCH /foo/posts/:id(.:format) foo/posts#update
PUT /foo/posts/:id(.:format) foo/posts#update
DELETE /foo/posts/:id(.:format) foo/posts#destroy
**Constraints & Redirect**
Run Code Online (Sandbox Code Playgroud)
Rails路由按顺序执行,您可以通过以下方式模拟条件登录:
match '/route' => 'controller#action', :constraints => Model.new
match '/route' => 'user#action'
Run Code Online (Sandbox Code Playgroud)
第一行检查是否满足约束条件(即,请求是否来自模型域).如果满足约束,则将请求路由到controller#action.
We can add constraints to routes for multiple uses like for ip-matching, params matching, restrict format parameter, request-based restrictions etc as :
- ip-matching
=> resources :model, constraints: { ip: /172\.124\.\d+\.\d+/ }
- filtering id params
=> match 'model/:id', to: 'model#show' ,constraints: { id: /\d+/}, via: :get
- restrict format params
=> match 'model/:id', to: 'model#show' ,constraints: { format: 'json' }, via: :get
- request-based constraints
=> get 'admin/', to: 'admin#show', constraints: { subdomain: 'admin' }
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6943 次 |
| 最近记录: |