2011-01-30 94 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 

我没有实际测试过,但应该很好地工作。