在Rails 3中:如何使用自定义格式在response_with中使用布局进行渲染? [英] In Rails 3: How do I render with layout in respond_with using a custom format?

查看:62
本文介绍了在Rails 3中:如何使用自定义格式在response_with中使用布局进行渲染?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题类似于该问题中的一个,但相反. 如何指定":layout =>错误"在Rails的response_with中?

My problem is similar to the one in this question, but the opposite. How do I specify ":layout => false" in Rails' respond_with?

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

The controller is sort of a file converter. It responds to many formats. One format is supposed to render an html layout showing metadata. This format need the layout to be rendered, unlike most other formats. Problem is I can't convince Rails to render it.

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"可以,但显然不行.

I thought ":layout => true" would work but apparently not.

如您所见,在将其粘贴到此处之前,我已经剥离了控制器中的所有其他内容.格式"fil"作为config/initializers/mime_types.rb中的text/html格式添加到mime类型.我已经将视图文件放置在适当的位置并且可以渲染,但是周围没有任何布局.

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. 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.

谢谢.

推荐答案

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

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'

这篇关于在Rails 3中:如何使用自定义格式在response_with中使用布局进行渲染?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆