Ruby-on-rails 关闭操作之一的布局
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2062312/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Turn off layout for one of action
提问by Antiarchitect
My situation: View action of ReportsController should render pure html, but not as a file (to view it in browser and save it after). So for rendering I use view template view.html.erb and i neet to turn off any layouts for this action. But in other actions of this controller layouts should stay untouched. Works only turning off for whole controller like this:
我的情况: ReportsController 的查看操作应该呈现纯 html,而不是作为文件(在浏览器中查看并保存)。因此,对于渲染,我使用视图模板 view.html.erb 并且我需要关闭此操作的任何布局。但在此控制器布局的其他操作中,应保持不变。仅适用于关闭整个控制器,如下所示:
ReportsController < ApplicationController
layout false
But that doing it wrong :( for all the actions I tried to use something like this in action:
但是这样做是错误的:(对于我试图在行动中使用这样的所有操作:
def view
@report = Report.new(params[:report])
unless @report.valid?
render :action => 'new' and return
else
render :layout => false
end
end
What should I do?
我该怎么办?
采纳答案by mckeed
Try this:
尝试这个:
ReportsController < ApplicationController
layout false
layout 'application', :except => :view
回答by David Ortiz
回答by Archonic
In the respond block, add layout: false.
在响应块中,添加layout: false.
For example:
例如:
respond_to do |format|
format.html { render :layout => false } # your-action.html.erb
end
回答by Mugur 'Bud' Chirica
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

