2014-09-30 18 views
0

我正在使用Rails 3應用程序。我有一些重要的實現(如設置租戶ID,身份驗證等)在我的應用程序的ApplicationController類(如過濾器)類。現在,當我嘗試使用Grape實現API時,我無法在Grape中重新使用applicationController邏輯。將ApplicationController繼承到Grape-API中

葡萄API類可以繼承ApplicationController嗎?如果我在這裏失去了一些東西,請教我。

謝謝。

回答

0

我不認爲你可以讓一個Rails控制器從Grape::API繼承。你可以做的是:創建你自己的基類API類,繼承Grape::API並實現必要的方法來模仿你的ApplicationController

葡萄:: API提供了一些回調掛鉤,所以你可以設置這些在你的基API類,像(未測試):

class MyAPI < Grape::API 
    before do 
    authenticate! 
    end 

    helpers do 
    def current_user 
     @current_user ||= User.authorize!(env) 
    end 

    def authenticate! 
     error!('401 Unauthorized', 401) unless current_user 
    end 
    end 
end 

class ProductAPI < MyAPI 
    # Your code here 
end