Rails路由:嵌套,成员,集合,命名空间,范围和可自定义
我想了解更多有关Rails路由的信息。
会员和collections
# 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
约束,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 }
定制:
resources :profiles
来自resource profiles
原始URL以进行编辑。
http://localhost:3000/profiles/1/edit
我想通过点击edit profile
为用户提供,并在下面查看url。
http://localhost:3000/profile/edit
此外,是否有高级路由,大多数大公司如何设计他们的铁路路线? 如果存在,我会很高兴看到新的路线。
谢谢 !
**Collection & Member routes**
成员路由需要ID,因为它作用于成员。
收集路由不需要ID,因为它作用于对象集合
:member
使用pattern /: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**
Rails
routes
中的namespace
和scope
会影响控制器名称,URI和命名路由。范围方法为您提供细粒度控制:
scope 'url_path_prefix', module: 'module_prefix', as: 'named_route_prefix' do resources :model_name end
For Example :
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
命名空间方法是一个简单的例子 – 它为所有内容添加前缀。
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路由按顺序执行,您可以通过以下方式模拟条件登录:
match '/route' => 'controller#action', :constraints => Model.new match '/route' => 'user#action'
第一行检查是否满足约束的条件(即,请求是否来自模型域)。 如果满足约束,则将请求路由到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' }
使用单一资源:
resource :profile
并在控制器中操纵当前用户的配置文件。
至于复杂的路由 – 通常是命名空间,只需要浅层路由和自定义操作的嵌套资源。
您可以通过这个答案来回答问题的第一部分。
回答你问题的第二部分。 您可以将“个人资料”视为您的单一资源(名词的单一性本身代表一个单一的资源)。 有关详细说明,请参阅此链接 。