【发布时间】: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 => '/layouts/mylayout' -
我试过“应用程序”和“不存在”。当我将其设置为不存在的布局时,我得到一个缺少布局的错误。当我将其设置为现有的时,我得到相同的空白结果。
标签: ruby-on-rails layout render