关闭一个动作的布局

我的情况: ReportsController 的 View 操作应该呈现纯粹的 html,而不是作为一个文件(在浏览器中查看它,然后保存它)。因此,对于渲染,我使用 view template view.html.erb,并且我需要关闭此操作的任何布局。但是在这个控制器的其他操作中,布局应该保持不变。 只能像这样关闭整个控制器:

ReportsController < ApplicationController
layout false

但是,做错了: (为所有的行动 我试图用这样的东西来实际操作:

def view
@report = Report.new(params[:report])
unless @report.valid?
render :action => 'new' and return
else
render :layout => false
end
end

我该怎么办?

70892 次浏览

Try this:

ReportsController < ApplicationController
layout false
layout 'application', :except => :view

This should do it,

def view
...
render :layout => false
end

Link to Documentation

In the respond block, add layout: false.

For example:

respond_to do |format|
format.html { render :layout => false }
end

If you want to get a non-standard template, with no layout you can use:

def non_rest
render template: 'layouts/something_new', layout: false
end