我的问题与这个问题中的问题相似,但相反。
How do I specify ":layout => false" in Rails' respond_with?
Controller 是一种文件转换器。它响应多种格式。一种格式应该呈现显示元数据的 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"会起作用,但显然不会。
如您所见,在将 Controller 粘贴到此处之前,我已经删除了 Controller 中的所有其他内容。格式“fil”作为 config/initializers/mime_types.rb 中的 text/html 格式添加到 mime 类型。我有我的 View 文件并渲染,但它周围没有任何布局。
我将不胜感激任何建议。我有一段时间无处可去,我在网上找到的任何东西都表明我的代码应该可以工作。
提前致谢。
最佳答案
发现如果我指定布局文件的确切路径和文件名,布局就会呈现。就我而言:
render :layout => '/layouts/application.html.haml'
关于ruby-on-rails - 在 Rails 3 : How do I render with layout in respond_with using a custom format?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3978800/