Rails 4 如何在视图中使用渲染布局选项? [英] Rails 4 How to use render layout option in view?
问题描述
只是搞乱 Rails 布局.浏览指南2.2.11.2 :layout 选项
Just messing around with rails layouts. Going through the guide 2.2.11.2 The :layout Option
创建一个名为 two_column_landing 的简单布局并将其放在 app/views 下的 layouts 目录中.
Created a simple layout called two_column_landing and put it in the layouts directory under app/views.
然后修改我的视图以呈现布局:
Then modified my view to render the layout:
<h1>Ca#tcl</h1>
<p>Find me in app/views/ca/tcl.html.erb</p>
<%= render layout: "two_column_landing" %>
我收到此错误:
You invoked render but did not give any of :partial, :template, :inline, :file or :text option.
我知道在生产代码中养成习惯可能不是一件好事,但我只是想知道如何做我想做的事.任何想法为什么这不起作用?难道不是rails 4的特性吗?
I know this is probably not a great thing to be doing as a habit in production code but I'm just figuring out how to do what I want. Any ideas why this isn't working? Is it not a feature of rails 4?
推荐答案
您可以继续阅读.有很多方法可以使用布局.如果要为给定操作调用特定布局,则应在控制器中执行此操作,而不是在视图中.如果你需要在一个视图中调用,给一个partial 一个layout,那么语法就不一样了,你先调用partial,然后再调用layout.
You might keep reading. There are many ways layout can be used. If you want to call a specific layout for a given action, you should do that in a your controller, not in your view. If you need to call in a view, to give a layout for a partial, then the syntax is different, you call the partial first and then the layout.
<%= render "comments", layout: "two_column_landing" %>
如果您只想在特定控制器中呈现 2 列视图,则在任何方法定义调用之前的控制器顶部,在类名下
If you just want you 2 column view to render in a particular controller then at the top of the controller before any method definitions call, under the class name
layout "two_column_landing"
如果您只想为控制器中的特定操作调用此布局,您可以在方法 render 中进行
If you want to only call this layout for a specific action in the controller you can do it in the method render
def index
@people = Person.all
render layout: "multi-column"
end
这篇关于Rails 4 如何在视图中使用渲染布局选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!