【问题标题】:How is it possible to set a default layout/no layout for certain MIME types in rails?如何为 rails 中的某些 MIME 类型设置默认布局/无布局?
【发布时间】:2011-11-14 16:42:31
【问题描述】:

我正在使用rabl 生成 json。设置 json 渲染它没有布局有点令人不安,可能不是正确的方法。它自然没有而且绝对不是基于 HTML 的。

如何让 Rails 不为 json 渲染添加布局?或者更一般地说,如何设置 MIME 类型布局的全局策略?

【问题讨论】:

    标签: ruby-on-rails json layout rabl


    【解决方案1】:

    我还没有检查过,但我认为它会起作用:

    class ApplicationController < BaseController
      layout :mime_layout
    
      def mime_layout
        case params[:format]
        when "json"
          false
        ....
        else
          "application"
        end
      end
    end
    

    【讨论】:

    • 为了完成,mime_layout 应该是一个私有方法。感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-18
    相关资源
    最近更新 更多