0

我遵循 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>
4

2 回答 2

1

您的布局文件的问题在于您有两个<title>标签,一个包含应用程序的标题,另一个是打开的。如果您删除一个未打开的,问题将得到解决。

于 2013-10-13T14:33:10.980 回答
0

您应该在控制器中填写应用程序布局。换句话说,添加 layout 'application'到您的控制器。例如:

class StaticPagesController < ApplicationController
    layout 'application'

    def home 
    end 

    def help 
    end

    def about 
    end 
end
于 2013-10-13T13:23:00.443 回答