小编典典

Rails:如何更改页面的标题?

all

在不使用插件的情况下为 Rails 应用程序中的页面创建自定义标题的最佳方法是什么?


阅读 116

收藏
2022-09-02

共1个答案

小编典典

在您看来,请执行以下操作:

<% content_for :title, "Title for specific page" %>
<!-- or -->
<h1><%= content_for(:title, "Title for specific page") %></h1>

布局文件中包含以下内容:

<head>
  <title><%= yield(:title) %></title>
  <!-- Additional header tags here -->
</head>
<body>
  <!-- If all pages contain a headline tag, it's preferable to put that in the layout file too -->
  <h1><%= yield(:title) %></h1>
</body>

也可以将content_forandyield(:title)语句封装在辅助方法中(正如其他人已经建议的那样)。但是,在这种简单的情况下,我喜欢将必要的代码直接放入特定的视图中,而无需自定义帮助程序。

2022-09-02