Rails:如何更改页面的标题? [英] Rails: How to change the title of a page?
本文介绍了Rails:如何更改页面的标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在不使用插件的情况下为 Rails 应用中的页面创建自定义标题的最佳方法是什么?
What is the best way to create a custom title for pages in a Rails app without using a plug-in?
推荐答案
在你的视图中做这样的事情:
In your views do something like this:
<% content_for :title, "Title for specific page" %>
<!-- or -->
<h1><%= content_for(:title, "Title for specific page") %></h1>
布局文件中包含以下内容:
The following goes in the layout file:
<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_for
和 yield(:title)
语句封装在辅助方法中(正如其他人已经建议的那样).但是,在这种简单的情况下,我喜欢将必要的代码直接放入特定的视图中,而无需自定义助手.
It's also possible to encapsulate the content_for
and yield(:title)
statements in helper methods (as others have already suggested). However, in simple cases such as this one I like to put the necessary code directly into the specific views without custom helpers.
这篇关于Rails:如何更改页面的标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文