Rails 3渲染问题

时间:2010-12-20 17:09:49

标签: yaml ruby-on-rails-3

我正在编写一个脚本,允许用户通过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

1 个答案:

答案 0 :(得分:1)

尝试:

在控制器中将:yaml添加到respond_to :yaml,并且:

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