2010-12-20 113 views
0

我正在编写一个脚本,允许用户通过URL参数传递格式。我有JSON和XML根据需要工作,但我不能让YAML工作。Rails 3呈现问题

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 

回答

1

尝试:

在控制器中添加:yamlrespond_to :yaml和:

respond_to do |format| 
    ....other formats.... 
    format.yaml { render :yaml => @labels_hash } 
end 
+1

我试过了,但它仍然试图强行下载的文件。 – dennismonsewicz 2010-12-20 19:01:24

+0

我用cURL调用了它。我试图在浏览器中查看创建的YAML,并且认为浏览器不会知道如何解释YAML MIME类型 – dennismonsewicz 2010-12-20 19:13:03