如何在 Rails 3 的 ActionController::Metal 中包含 Responder?

发布于 2024-09-05 10:18:24 字数 675 浏览 10 评论 0原文

我正在使用一个 Rails 3 控制器,该控制器的用途非常具体、有限,我所需要的就是让它响应 :json。

该截屏视频表示我的控制器可以从 ActionController::Metal 继承,然后只包含我需要的功能以加快速度: http://rubyonrails.org/screencasts/rails3/action-controller

当我的控制器看起来像这样:

class FoldersController < ActionController::Metal
  respond_to :json
  def all
    respond_with Folder.all
  end
end

我收到错误:

undefined method `respond_to' for FoldersController:Class

我尝试过包括 Responder、ActionController::Responder、ActionController::Metal::Responder,但它们都不起作用。我需要包含哪些内容才能获得此响应程序功能?

I'm working a Rails 3 controller that has a very specific, limited purpose, and all I need is for it to respond_to :json.

This screencast says my controller can inherit from ActionController::Metal, and then just include the functionality I need to make things faster:
http://rubyonrails.org/screencasts/rails3/action-controller

When my controller looks like this:

class FoldersController < ActionController::Metal
  respond_to :json
  def all
    respond_with Folder.all
  end
end

I get the error:

undefined method `respond_to' for FoldersController:Class

I've tried including Responder, ActionController::Responder, ActionController::Metal::Responder, but none of them work. What do I include to get this responder functionality?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风苍溪 2024-09-12 10:18:24

您需要包含更多类,而不仅仅是 Responder。这是我的 ApplicationController,但并非全部包含您可能需要的:

class Api::ApplicationController < ActionController::Metal
  include ActionController::Helpers
  include ActionController::UrlFor

  include ActionController::Redirecting
  include ActionController::Rendering           # enables rendering
  include ActionController::ConditionalGet      # required for respond_to and respond_with
  include ActionController::MimeResponds        # enables serving different content types like :xml or :json

  include ActionController::Cookies             # enables cookies
  include AbstractController::Callbacks         # callbacks for your authentication logic
  include ActiveSupport::Rescuable              # enables resque_from

  include Rails.application.routes.url_helpers
end

You need to include more classes, not only Responder. Here is my ApplicationController, but not all includes you may need:

class Api::ApplicationController < ActionController::Metal
  include ActionController::Helpers
  include ActionController::UrlFor

  include ActionController::Redirecting
  include ActionController::Rendering           # enables rendering
  include ActionController::ConditionalGet      # required for respond_to and respond_with
  include ActionController::MimeResponds        # enables serving different content types like :xml or :json

  include ActionController::Cookies             # enables cookies
  include AbstractController::Callbacks         # callbacks for your authentication logic
  include ActiveSupport::Rescuable              # enables resque_from

  include Rails.application.routes.url_helpers
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文