具有API和Web视图的Rails 4应用程序中的ActionController :: API

Bru*_*cca 7 api ruby-on-rails rails-api

我有一个带有Web视图和JSON API的Rails 4.2.7应用程序。Web视图和API有单独的基本控制器。我希望API基本控制器继承自ActionController::API。但是,在Rails 4中ActionController::API是在rails-apigem中实现的,而我使用的是标准railsgem。

我试图创建一个ActionController::API“克隆”从继承ActionController::Metal和包括所包含所有模块ActionController::API(见源rails-api 在这里rails5 这里)。结果代码为:

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
Run Code Online (Sandbox Code Playgroud)

它似乎正在工作,但我想与经验更丰富的开发人员一起验证此解决方案。可以用它代替ActionController::API吗?

提前致谢。