關(guān)于Ruby on Rails路由配置的一些建議
當(dāng)你需要加入一個(gè)或多個(gè)動(dòng)作至一個(gè) RESTful 資源時(shí)(你真的需要嗎?),使用 member and collection 路由。
# 差 get 'subscriptions/:id/unsubscribe' resources :subscriptions # 好 resources :subscriptions do get 'unsubscribe', on: :member end # 差 get 'photos/search' resources :photos # 好 resources :photos do get 'search', on: :collection end
若你需要定義多個(gè) member/collection 路由時(shí),使用替代的區(qū)塊語法(block syntax)。
resources :subscriptions do member do get 'unsubscribe' # 更多路由 end end resources :photos do collection do get 'search' # 更多路由 end end
使用嵌套路由(nested routes)來更佳地表達(dá)與 ActiveRecord 模型的關(guān)系。
class Post < ActiveRecord::Base has_many :comments end class Comments < ActiveRecord::Base belongs_to :post end # routes.rb resources :posts do resources :comments end
使用命名空間路由來群組相關(guān)的行為。
namespace :admin do # Directs /admin/products/* to Admin::ProductsController # (app/controllers/admin/products_controller.rb) resources :products end
不要在控制器里使用留給后人般的瘋狂路由(legacy wild controller route)。這種路由會(huì)讓每個(gè)控制器的動(dòng)作透過 GET 請(qǐng)求存取。
# 非常差 match ':controller(/:action(/:id(.:format)))'
相關(guān)文章
使用Ruby來編寫訪問Twitter的命令行應(yīng)用程序的教程
這篇文章主要介紹了使用Ruby來編寫訪問Twitter的命令行應(yīng)用程序的教程,文章來自于IBM官方網(wǎng)站技術(shù)文檔,需要的朋友可以參考下2015-04-04Ruby使用Monkey Patch猴子補(bǔ)丁方式進(jìn)行程序開發(fā)的示例
Monkey Patch猴子補(bǔ)丁是指在程序解釋運(yùn)行時(shí)動(dòng)態(tài)添加類或模塊的做法,這里我們就來看一下Ruby使用Monkey Patch猴子補(bǔ)丁方式進(jìn)行程序開發(fā)的示例2016-05-05