【问题标题】:In Rails 3: How do I render with layout in respond_with using a custom format?在 Rails 3 中:如何使用自定义格式在respond_with 中呈现布局?
【发布时间】:2010-10-20 14:14:51
【问题描述】:

我的问题与这个问题中的问题相似,但恰恰相反。 How do I specify ":layout => false" in Rails' respond_with?

控制器是一种文件转换器。它响应多种格式。一种格式应该呈现显示元数据的 html 布局。与大多数其他格式不同,此格式需要渲染布局。问题是我无法说服 Rails 渲染它。

class CustomFilesController < ApplicationController
  respond_to :all, :only => :show
  def show
    @custom_file = CustomFile.find(params[:id])
    respond_with(@custom_file) do |format|
      format.html {}
      format.xml {}
      format.json {}
      format.fil {
        puts '>>>>> IN FIL <<<<<'
        render :layout => true
      }
      format.any {
        file = @custom_file.as(:type => params[:format], :size => params[:size]) ## the REAL path to the file
        (file.nil? || @custom_file.nil?) ? head(:not_found) : send_file(file, :disposition => 'inline', :url_based_filename => true)
      }
    end
  end
end

我认为 ":layout => true" 会起作用,但显然不行。

如您所见,在将其粘贴到此处之前,我已经剥离了控制器中的所有其他内容。格式“fil”在 config/initializers/mime_types.rb 中作为 text/html 格式添加到 mime 类型中。我有我的视图文件并且它呈现,但它周围没有任何布局。

如果有任何建议,我将不胜感激。我已经有一段时间无处可去了,我在网上找到的任何东西都表明我的代码应该可以工作。

提前致谢。

【问题讨论】:

  • 您是否尝试指定布局的路径? :layout =&gt; '/layouts/mylayout'
  • 我试过“应用程序”和“不存在”。当我将其设置为不存在的布局时,我得到一个缺少布局的错误。当我将其设置为现有的时,我得到相同的空白结果。

标签: ruby-on-rails layout render


【解决方案1】:

发现如果我指定布局文件的确切路径和文件名,布局就会呈现。就我而言:

render :layout => '/layouts/application.html.haml'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-05
    • 2016-10-09
    相关资源
    最近更新 更多