Rails 3 渲染问题

发布于 2024-10-08 04:25:52 字数 697 浏览 0 评论 0原文

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

I am writing a script that allows for a user to pass a format via a URL parameter. I have JSON and XML working as needed, but I can't get YAML working.

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

For some reason when I pass the format=yaml in my URL then my script tries to force download a file. Any reason why this would happen?

Working Code:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

飘逸的'云 2024-10-15 04:25:52

尝试:

:yaml 添加到控制器中的 respond_to :yaml 中,然后:

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

Try:

Adding :yaml to respond_to :yaml in the controller, and :

respond_to do |format|
  ....other formats....
  format.yaml { render :yaml => @labels_hash }
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文