将rails限制为仅响应JSON和XML请求

Nic*_*ick 7 api web-services ruby-on-rails

有没有办法全局定义rails应用程序只提供json和xml以及任何其他请求的适当错误?

我认为它与ApplicationController中的before_filter和responds_to块一致,但就我的调查而言,这就是我.

edg*_*ner 10

只需在控制器的类级别声明它,使用respond_to.如果你这样做,它将适用于你的所有控制器ApplicationController

class ApplicationController < ActionController::Base
  respond_to :xml, :json
  …
end
Run Code Online (Sandbox Code Playgroud)

另请阅读有关ActionController::Responder更多选项的课程.