我正在使用Rails 3应用程序。在我的应用程序的ApplicationController(如过滤器之前)类中,我没有几个重要的实现(例如,设置租户ID,身份验证等)。现在,当我尝试使用Grape来实现API时,我无法重用Grape中的applicationController逻辑。
Grape API类是否可以继承ApplicationController?以防万一,如果我在这里缺少什么,请教育我。
谢谢。
我认为您不能让Rails控制器继承自Grape::API
。您可以做的是:创建自己的基础API类,该类继承自Grape::API
并实现必要的方法以模仿您的ApplicationController
。
Grape :: 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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句