我遵循 Michael Hartl 的 Ruby on Rails 教程,但我遇到了一个不适合我的示例。我尝试从 .erb 文件中删除重复代码,以便该代码仅存在于 application.html.erb 文件中。使用旧的 home.html.erb 文件一切正常(当我为 home 执行 GET 时,会显示内容),但是对于我应该用来消除重复代码的文件,没有显示任何内容。经过测试,我发现即使从旧文件中删除标题标签也足以使内容消失。
任何想法为什么会这样?教程是错误的还是我错过了什么?
应用程序.html.erb:
<!DOCTYPE html>
<html>
<head>
<title>Title | <%= yield(:title) %></title>
<title>
<%= stylesheet_link_tag "application", :media => "all" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
</head>
<body>
<%= yield %>
</body>
</html>
主页.html.erb
<% provide(:title, 'Home') %>
<h1>Sample App</h1>
<p>
This is the home page for the
<a href="http://railstutorial.org/">Ruby on Rails Tutorial</a>
sample application.
</p>
旧 home.html.erb:
<% provide(:title, 'Home') %>
<!DOCTYPE html>
<html>
<head>
<title>Title | <%= yield(:title) %></title>
</head>
<body>
<h1>Sample App</h1>
<p>
This is the home page for the
<a href="http://railstutorial.org/">Ruby on Rails Tutorial</a>
sample application.
</p>
</body>
</html>