简体   繁体   English

在Rails 3中:如何使用自定义格式在response_with中使用布局进行渲染?

[英]In Rails 3: How do I render with layout in respond_with using a custom format?

My problem is similar to the one in this question, but the opposite. 我的问题类似于此问题中的问题,但相反。 How do I specify ":layout => false" in Rails' respond_with? 如何在Rails的response_with中指定“:layout => false”?

The controller is sort of a file converter. 该控制器有点像文件转换器。 It responds to many formats. 它响应多种格式。 One format is supposed to render an html layout showing metadata. 一种格式应该呈现一个显示元数据的html布局。 This format need the layout to be rendered, unlike most other formats. 与大多数其他格式不同,此格式需要呈现布局。 Problem is I can't convince Rails to render it. 问题是我无法说服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

I thought ":layout => true" would work but apparently not. 我认为“:layout => true”会起作用,但显然不起作用。

As you can see I have stripped away everything else in the controller before pasting it in here. 如您所见,在将其粘贴到此处之前,我已经剥离了控制器中的所有其他内容。 The format "fil" is added to the mime types as a text/html format in config/initializers/mime_types.rb. 格式“ fil”作为config / initializers / mime_types.rb中的text / html格式添加到mime类型。 I have my view file in place and it renders, but without any layout around it. 我将视图文件放置在适当的位置并进行渲染,但是周围没有任何布局。

I would appreciate any advice. 我将不胜感激任何建议。 I have gotten nowhere for a while now and anything I find online suggests my code should work. 我已经有一段时间没有了,网上发现的任何东西都表明我的代码应该可以工作。

Thanks in advance. 提前致谢。

Found that the layout renders if I specify the exact path and filename of the layout file. 发现如果我指定布局文件的确切路径和文件名,则布局将呈现。 In my case: 就我而言:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM