2016-09-27 63 views
0

(紅寶石2.3.1,Rails的5.0.0.1)導軌和版本API:版本幫手名

我已經在URI中實現的API版本,並加入設計令牌身份驗證是這樣的:

constraints subdomain: 'api' do 
    scope module: 'api' do 
     namespace :v1 do 
      # V1 stuff 
      resources :users 
      mount_devise_token_auth_for 'User', at: 'auth' 
     end 
    end 
end 

「耙路線」(例如行)的結果因此:

v1_user GET /v1/users/:id(.:format) api/v1/users#show {:subdomain=>"api"} 

現在在我的控制器,與該用戶相關的任何幫助包含「V1」版本。例如:

before_action :authenticate_v1_user! 

我是新來的這個概念,但這不應該避免嗎?如果我將升級到v2,我必須通過所有這些助手,並確保我會改變它,以防止它打破?或者升級過程如何?

感謝您的反饋!這可能是一個非常簡單的問題,但我有點困惑。

+0

我會避免使用版本直到你至少有兩個不同的版本...所以只要把它稱爲'authenticate_user!',直到你有兩種不同的認證方式,那麼你可以有'authenticate_v1_user'和'authenticate_v2_user'來區分...雖然tbh我' d使用'roles'處理不同的處理方式(仍然保持v1等)。 –

回答

0

好了,我已經把搜索了一下,發現一個解決方案,它的工作原理(即使它感覺有點太「哈克」對我的口味):

namespace :v1, as: '' do 
    # V1 stuff 
    resources :users 
    mount_devise_token_auth_for 'User', at: 'auth' 
end 

所以as: ''的伎倆。 (答案摘自:Adding prefix to a named route helper under namespace

如果我曾經想要的「V1」回到我的助手的名字,我想我可以手動例如添加它們每路線:as: 'new_v1_user_sesssion'