Rails路由:嵌套,成员,集合,命名空间,范​​围和可自定义

时间:2016-04-03 10:29:20

标签: ruby-on-rails ruby ruby-on-rails-4 routing routes

我想了解更多有关Rails路由的信息。

会员和收藏

  # 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的原始网址进行修改。

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

我想通过点击edit profile向用户提供,并在下方查看网址。

http://localhost:3000/profile/edit

此外,是否有高级路由,大多数大公司如何设计他们的铁路路线?如果有的话,我会很高兴看到新的路线。

谢谢!

2 个答案:

答案 0 :(得分:2)

使用单一资源:

resource :profile

并在控制器中操纵当前用户的配置文件。

对于复杂路由 - 通常是名称空间,只需要浅层路由和自定义操作的嵌套资源。

答案 1 :(得分:1)

您可以通过this回答问题的第一部分。

回答你问题的第二部分。你可以对待" profile"作为你的独特资源(名词本身的奇点代表一种奇异的资源)。有关详细说明,请参阅此link