我正在编写一个脚本,允许用户通过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由于某些原因,当我在URL中传递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发布于 2010-12-21 02:19:00
尝试:
将:yaml添加到控制器中的respond_to :yaml,并且:
respond_to do |format|
....other formats....
format.yaml { render :yaml => @labels_hash }
endhttps://stackoverflow.com/questions/4492011
复制相似问题