ruby on rails 如何在没有布局和其他头部字段的情况下渲染页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15949319/
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
ruby on rails how to render a page without layout and other head field
提问by mewosic
else
respond_to do |format|
format.html { render "tabelle/show" }
end
end
I want to render the page ...with only the code in that page....not add <head>...layout and <body>field in ruby on rails.
I only want to show the result of code in the page tabelle/show.html.haml
我想渲染页面......只有该页面中的代码......不要在轨道上的ruby中添加<head>......布局和<body>字段。我只想在页面 tabelle/show.html.haml 中显示代码的结果
回答by fmendez
You can do it like this:
你可以这样做:
format.html { render "tabelle/show", :layout => false }
回答by HungryCoder
add
添加
:layout => false
Example:
例子:
render "tabelle/show", :layout => false
回答by Fabricioblz
Controller:
控制器:
layout false, only: [:method_name]
this is very useful when you using render_to_string
这在您使用 render_to_string 时非常有用
回答by Joshua Pinter
If you do not want to specify the view to use.
如果您不想指定要使用的视图。
Rails is smart enough to know which view template to use based on the Controller Action you're on.
Rails 足够聪明,可以根据您所在的控制器操作知道要使用哪个视图模板。
For example, if you're on the showaction of the TabellesControlleryou wouldn't need to specify render "tabelle/show"in your Controller Action because Rails will already assume that and will automatically try to render the file in app/views/tabelles/show.html.erb.
例如,如果您使用的是 的show操作,则TabellesController不需要render "tabelle/show"在控制器操作中指定,因为 Rails 已经假定并会自动尝试在app/views/tabelles/show.html.erb.
So if you're sticking with all of those defaults then you can just use the following to render withoutthe typical layout template:
因此,如果您坚持使用所有这些默认值,那么您只需使用以下内容即可在没有典型布局模板的情况下进行渲染:
def show
# Other stuff in your Controller Action.
render layout: false
end
This will render app/views/tabelles/show.html.erbbut without the layout template automatically.
这将app/views/tabelles/show.html.erb自动呈现但没有布局模板。
Noice.
诺斯。

