2014-02-18 61 views
1

將我的項目與第三方項目集成時,會出現一個很大的衝突。Rails路由 - 範圍正則表達式或重定向

我使用的模型複數路線名稱,例如:

http://my_app/users/search (plural) 

雖然他們使用

http://my_app/user/search (singular) 

這種模式被用於通過4種型號流傳我不19條不同的路線真的不想複製每條路線來支持其他項目。

有沒有辦法在範圍上使用正則表達式或重定向來避免代碼重複?

目前我有:

... 
scope 'users' do 
    get 'search' 
    scope 'id' do 
    get '' 
    get 'ping' 
    end 
end 
... 

我希望這樣的事情:

... 
scope 'user(s)?' do 
    get 'search' 
    scope 'id' do 
    get '' 
    get 'ping' 
    end 
end 

或者:

scope 'user', to: redirect('users') 

回答

5
scope ':pattern', constraints: { pattern: /user(s)?/ } do 
    ... 
end 
+1

沒有必要使用括號,你可以使用'/ users?/'regexp – trushkevich