代码
respond_to do |format|
format.html
format.json { render :json => @switches }
format.xml { render :xml => @switches.to_xml }
format.all { render :text => "only HTML, XML, and JSON format are supported at the moment." }
end
在Rails 2.2.2中,上述方法是可行的。但在Rails 3中,浏览器访问controller/index.html或者index时都会落入最后一行的提示:"目前仅支持HTML和JSON格式。"
我所能找到的唯一关于此问题的Rails文档是:
目前该文档只有以下内容:
respond_to :html, :xml, :json
但是它们需要针对json和xml分别使用不同的模板,并且无法处理“目前仅支持HTML和JSON格式”的情况。
respond_to
示例(并进行了澄清)。@dreeves:对我来说,它只是起作用了,你提到的博客文章在Rails术语中已经过时了 :) - nathanvdaformat.any
,但我将其编辑为format.all
,因为format.any
是另一种不同的东西,它会接受参数,例如format.any(:html, :json)
。 - Dorian