2011-01-30 125 views
0

我有這個在我的路線:重構的Rails 3路由

get '/boutique/new' => 'stores#new', :as => :new_store, :constraints => { :id => /[a-z0-9_-]/ } 
post '/boutique' => 'stores#create', :as => :create_store, :constraints => { :id => /[a-z0-9_-]/ } 
get '/:shortname' => 'stores#show', :as => :store, :constraints => { :id => /[a-z0-9_-]/ } 
get '/:shortname/edit' => 'stores#edit', :as => :edit_store, :constraints => { :id => /[a-z0-9_-]/ } 
put '/:shortname' => 'stores#update', :as => :update_store, :constraints => { :id => /[a-z0-9_-]/ } 
delete '/:shortname' => 'stores#delete', :as => :destroy_store, :constraints => { :id => /[a-z0-9_-]/ } 

有一個更清潔的方式做同樣的?它看起來沒有任何優雅,甚至更少,如果我添加更多的控制/行動。

謝謝。

回答

2

你最好的選擇是堅持the standard resource routes。如果其他人需要處理你正在構建的應用程序,他們會感謝你。

這就是說,如果你真的需要該路由設置(無論何種原因),請嘗試以下操作:

controller :stores do 
    constraints :id => /[a-z0-9_-]/ do 
    get '/boutique/new' => :new, :as => :new_store 
    post '/boutique'  => :create, :as => :create_store 
    get '/:shortname'  => :show, :as => :store 
    get '/:shortname/edit' => :edit, :as => :edit_store 
    put '/:shortname'  => :update, :as => :update_store 
    delete '/:shortname'  => :delete, :as => :destroy_store 
    end 
end 

我沒有實際測試過,但應該很好地工作。