Rails:如何更改页面标题?

ush

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

Christoph Schiessl

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

<% 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)语句封装在辅助方法中(正如其他人已经建议的那样)。但是,在这种简单的情况下,我喜欢将必要的代码直接放入特定视图中,而无需自定义帮助程序。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章