繁体   English   中英

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

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

我的问题类似于此问题中的问题,但相反。 如何在Rails的response_with中指定“:layout => false”?

该控制器有点像文件转换器。 它响应多种格式。 一种格式应该呈现一个显示元数据的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类型。 我将视图文件放置在适当的位置并进行渲染,但是周围没有任何布局。

我将不胜感激任何建议。 我已经有一段时间没有了,网上发现的任何东西都表明我的代码应该可以工作。

提前致谢。

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

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

暂无
暂无

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

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