Rails路线:嵌套,成员,集合,名称空间,范围和可自定义 [英] Rails routes: Nested, Member, Collection, namespace, scope and customizable

查看:66
本文介绍了Rails路线:嵌套,成员,集合,名称空间,范围和可自定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解有关Rails路线的更多信息。

I am trying to understand more about Rails routes.

会员和收藏

  # Example resource route with options:
     resources :products do
       member do
         get 'short'
         post 'toggle'
       end

       collection do
         get 'sold'
       end
     end

名称空间和范围

  # Example resource route within a namespace:
     namespace :admin do
       resources :products
     end

     scope :admin do
       resources :products
     end

约束,重定向到

# 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
 }






自定义:

resources :profiles

来自资源配置文件的原始URL进行编辑。

original url from resource profiles for edit.

http://localhost:3000/profiles/1/edit

我想通过单击编辑个人资料使其对用户可用,并在下面看到类似的网址。

I want to make it for users available only through click edit profile and see url like in below.

http://localhost:3000/profile/edit






还有,是否有高级布线,大多数大公司如何设计它们在铁轨中的布线?如果存在的话,我将非常高兴。


Also, is there advanced routing, How most big companies design their routes in rails ? I would be really glad to see new kind of routes if there exist.

谢谢!

推荐答案

**Collection & Member routes**





  • 成员路线需要ID,因为它作用于成员。

  • A member route requires an ID, because it acts on a member.

集合路线不需要ID,因为它作用于
对象集合

A collection route doesn't require an ID because it acts on a collection of objects

:member 使用模式 /:controller /:id /:your_method

:collection /:controller /:your_method

例如:

map.resources :users, :collection => { :abc => :get } => /users/abc
map.resources :users, :member => { :abc => :get } => /users/1/abc

**Scopes & Namespaces routes**




命名空间路由中的c $ c>和范围影响控制器
的名称,URI和命名路由。

namespace and scope in the Rails routes affect the controller names, URIs, and named routes.

范围方法为您提供细粒度的控制:



scope 'url_path_prefix', module: 'module_prefix', as: 'named_route_prefix' do
  resources :model_name
end

例如:

scope 'foo', module: 'bar', as: 'baz' do
  resources :posts
end

将路线生成为:

  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




命名空间方法是一个简单的例子—它为所有内容加上前缀。

The namespace method is the simple case — it prefixes everything.



namespace :foo do
  resources :posts
end

将路线生成为:

   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**




Rails路由是按顺序执行的,您可以模仿条件
以以下方式登录:

Rails routes are executed sequentially, you can mimic conditional login in the following manner:



match '/route' => 'controller#action', :constraints => Model.new
match '/route' => 'user#action'

第一行检查是否满足约束条件(即,是否该请求是从Model域发出的)。如果满足约束,则将请求路由到controller#action。

The first line checks whether the conditions of the constraint are met (i.e., if the request is emanating from a Model domain). If the constraint is satisfied, the request is routed to 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' }

这篇关于Rails路线:嵌套,成员,集合,名称空间,范围和可自定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆