我正在学习ruby on rails教程,我在用户视图<% provide(:title, 'Sign up') %>中看到了以下内容,只是想知道它到底在做什么?
发布于 2013-02-07 04:32:33
这应该会回答你的问题:
content_for?
只需使用#content_for检查是否已捕获任何内容
根据视图中的内容以不同的方式呈现布局的各个部分非常有用。
示例
如果没有#content_for :right_column,您可能会在布局中使用不同的css。
<%# This is the layout %>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>My Website</title>
<%= yield :script %>
</head>
<body class="<%= content_for?(:right_col) ? 'one-column' : 'two-column' %>">
<%= yield %>
<%= yield :right_col %>提供(名称,内容=空,块)(&B)
与content_for相同,但与流式处理一起使用时,会直接刷新回布局。换句话说,如果您希望在呈现给定模板时多次连接到同一缓冲区,则应使用content_for,否则使用provide通知布局停止查找更多内容。
简短回答:在Ruby on Rails教程中,提供的特殊rails函数用于在每个页面上设置不同的标题。根据您所在的static_page,标题会相应地设置。Ruby on Rails教程中的3.3.3 ("Embedded Ruby")中给出了进一步的解释。
https://stackoverflow.com/questions/14738014
复制相似问题