我正在编写一个脚本,允许用户通过URL参数传递格式。我根据需要使用JSON和XML,但我不能让YAML工作。
case params[:format]
when "xml" then respond_with(@labels)
when "json" then respond_with(@labels_hash.to_json)
when "yaml" then render :text => @labels_hash.to_yaml
end
出于某种原因,当我在我的网址中传递format=yaml
时,我的脚本会尝试强制下载文件。这会发生什么原因?
工作代码:
case params[:format]
when "xml" then respond_with(@labels)
when "json" then respond_with(@labels_hash.to_json)
when "yaml" then respond_with(@labels_hash) do |format|
format.yaml { render :text => @labels_hash.to_s }
end
end
答案 0 :(得分:1)
尝试:
在控制器中将:yaml
添加到respond_to :yaml
,并且:
respond_to do |format|
....other formats....
format.yaml { render :yaml => @labels_hash }
end