respond_to的通常用法如下
respond_to do |format|
format.html
format.xml { render :xml => @data }
end当不支持该格式时(比如上面的json或csv不支持),是否可以返回一个文本行来说明“该格式不受支持”,或者更好的是,让它自动报告“只支持html和xml”?它只能知道现有的format.html和format.xml行只支持html和xml。(如果可能)
发布于 2010-08-28 06:59:13
您应该能够使用format.all
respond_to do |format|
format.html
format.xml { render :xml => @data }
format.all { render :text=>'the format is not supported' }
end如果要列出支持的格式,则需要扩展响应器类。
将此代码放入类似于config/initializers/extend_responder.rb的内容中
module ActionController
module MimeResponds
class Responder
def valid_formats
@order.map(&:to_sym)
end
end
end
end然后在你的控制器中使用这个:
respond_to do |format|
format.html
format.json { render :text=>'{}' }
format.all { render :text=>"only #{(format.valid_formats - [:all]).to_sentence} are supported" }
endhttps://stackoverflow.com/questions/3587426
复制相似问题