我不知道这样做的最佳方法是什么。
在我的application.html.erb上我定义了一个标题div。
我的默认控制器(root)是主页控制器。 我希望如果我在主页的索引处标题是使用某些内容呈现的,但所有其他控制器都会在该标题内呈现另一个内容。
如何在该标题div中创建一个条件,以根据正在呈现的控制器呈现不同的内容?
I don't know what's the best way to doing this.
On my application.html.erb I define a header div.
My default controller ( root ) is a homepage controller. And I wish that if I'm at index of the homepage the header is rendering with some content, but all other controllers render another content inside that header.
How can I make a condition in that header div to render different content based on the controller that's being rendered?
最满意答案
您可以使用content_for帮助程序在从应用程序布局中生成的各个视图模板中定义标记块。
application.html.erb :
<div id="header"> <%= yield :header %> </div>然后在控制器的视图模板中的任何位置包含这样的代码:
<% content_for :header do %> This is a controller-specific header. <% end %>请注意,您可以根据需要定义任意数量的块,因此您也可以使用条件侧边栏等。
Layouts和content_for RailscastYou can use the content_for helper to define a block of markup in individual view templates that's yielded to from the application layout.
application.html.erb:
<div id="header"> <%= yield :header %> </div>Then include code like this anywhere within a controller's view template:
<% content_for :header do %> This is a controller-specific header. <% end %>Note that you can define as many of these blocks as you like, so you could have a conditional sidebar etc. as well.
Layouts and content_for Railscast更多推荐
发布评论