我有一个包含Web视图和JSON API的Rails 4.2.7应用程序。 Web视图和API有单独的基本控制器。我希望API基本控制器继承ActionController::API
。但是,在Rails 4中ActionController::API
在rails-api
gem中实现,我正在使用标准的rails
gem。
我尝试通过继承ActionController::API
并包含ActionController::Metal
中包含的所有模块来创建ActionController::API
“克隆”(请参阅rails-api
{{的来源3}}和rails
5 here)。生成的代码是:
class Api::V1::BaseApiController < ActionController::Metal
include AbstractController::Rendering
include ActionController::UrlFor
include ActionController::Redirecting
# originally ApiRendering
include ActionController::Rendering
include ActionController::Renderers::All
include ActionController::ConditionalGet
include ActionController::RackDelegation
# Originally BasicImplicitRender
include ActionController::ImplicitRender
include ActionController::StrongParameters
include ActionController::ForceSSL
include ActionController::DataStreaming
# Before callbacks should also be executed as early as possible so
# also include them at the bottom.
include AbstractController::Callbacks
# Append rescue at the bottom to wrap as much as possible.
include ActionController::Rescue
# Params wrapper should come before instrumentation so they are
# properly showed in logs
include ActionController::ParamsWrapper
# Add instrumentations hooks at the bottom to ensure they instrument
# all the methods properly.
include ActionController::Instrumentation
# base API controller code here
end
它似乎有效,但我想与更有经验的开发人员验证此解决方案。可以用它来代替ActionController::API
吗?
提前致谢。