简体   繁体   English

ruby on rails如何渲染没有布局和其他头部字段的页面

[英]ruby on rails how to render a page without layout and other head field

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. 我想渲染页面...只有该页面中的代码....不要在rails上的ruby中添加<head> ... layout和<body>字段。 I only want to show the result of code in the page tabelle/show.html.haml 我只想在页面tabelle / show.html.haml中显示代码的结果

你可以这样做:

format.html { render "tabelle/show", :layout => false  } 

add

:layout => false

Example: 例:

render "tabelle/show", :layout => false

Controller: 控制器:

layout false, only: [:method_name]

this is very useful when you using render_to_string 这在使用render_to_string时非常有用

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非常聪明,可以根据您所在的Controller Action知道要使用哪个视图模板。

For example, if you're on the show action of the TabellesController you 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 . 例如,如果您正在使用TabellesControllershow动作,则不需要在Controller Action中指定render "tabelle/show" ,因为Rails已经假设并且将自动尝试在app/views/tabelles/show.html.erb呈现文件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 without the typical layout template: 因此,如果您坚持使用所有这些默认值,那么您可以使用以下内容进行渲染, 而无需使用典型的布局模板:

def show
  # Other stuff in your Controller Action.

  render layout: false
end

This will render app/views/tabelles/show.html.erb but without the layout template automatically. 这将呈现app/views/tabelles/show.html.erb但不会自动显示布局模板。

Noice. 在降噪。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM