Ruby-on-rails Rails:如何更改页面的标题?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/185965/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rails: How to change the title of a page?
提问by wusher
What is the best way to create a custom title for pages in a Rails app without using a plug-in?
在不使用插件的情况下为 Rails 应用程序中的页面创建自定义标题的最佳方法是什么?
回答by Christoph Schiessl
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>
It's also possible to encapsulate the content_forand 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.
也可以将content_forandyield(:title)语句封装在辅助方法中(正如其他人已经建议的那样)。然而,在这种简单的情况下,我喜欢将必要的代码直接放入特定的视图中,而无需自定义帮助程序。
回答by opsb
Here's a simple option that I like to use
这是我喜欢使用的一个简单选项
In your layout
在您的布局中
<head>
<title><%= @title %></title>
</head>
And at the top of your page template (first line)
在页面模板的顶部(第一行)
<% @title="Home" %>
Because of the way the layout and page templates are parsed the @title="Home" is evaluated before the layout is rendered.
由于布局和页面模板的解析方式,@title="Home" 在布局呈现之前被评估。
回答by Aupajo
Best practice is to use content_for.
最佳实践是使用 content_for。
First, add a couple of helper methods (ie. stick in app/helpers/application_helper.rb):
首先,添加几个辅助方法(即坚持在 app/helpers/application_helper.rb 中):
def page_title(separator = " –?")
[content_for(:title), 'My Cool Site'].compact.join(separator)
end
def page_heading(title)
content_for(:title){ title }
content_tag(:h1, title)
end
Then in your layout view you can simply use:
然后在您的布局视图中,您可以简单地使用:
<title><%= page_title %></title>
...and in the view itself:
...在视图本身中:
<%= page_heading "Awesome" %>
This way has the advantage of allowing you to shuffle where you stick the h1 tag for your title, and keeps your controller nice and free of pesky @title variables.
这种方式的优点是让您可以随意调整标题的 h1 标签,并使您的控制器保持良好状态并且没有讨厌的 @title 变量。
回答by boulder_ruby
An improvement on @opsband a more complete form of @FouZ's:
对@opsb的改进和更完整的@FouZ形式:
In application.html.erb:
在application.html.erb:
<title><%= @title || "Default Page Title" %></title>
In the view erb file or its controller:
在查看 erb 文件或其控制器中:
<% @title = "Unique Page Title" %>
回答by Avdi
Look into content_for: http://railscasts.com/episodes/8
查看content_for:http: //railscasts.com/episodes/8
回答by Alan
Without further details on the use-case or requirements that you're trying to satisfy, I can think of several alternatives:
如果没有关于您要满足的用例或要求的更多详细信息,我可以想到几种替代方法:
1) Switch the title in one of your layout pages and consume a helper method stored in application_helper.rb
1)在您的布局页面之一中切换标题并使用存储在 application_helper.rb
<title><%= custom_title %></title>
This approach will give you a unique title for each layout page.
这种方法将为每个布局页面提供一个唯一的标题。
2) Railscasts suggests using a partial to load what shows up between the HEAD tags
2) Railscasts 建议使用部分加载 HEAD 标签之间显示的内容
3) Use javascript/ajax calls to manipulate the DOM if you need to change the title after the load event.
3) 如果您需要在加载事件后更改标题,请使用 javascript/ajax 调用来操作 DOM。
Maybe you don't really want to change the content tagged by the titleelement. Perhaps you really need a breadcrumb of some sort, so that your users always know where they are with respect to your site's navigation hierarchy. While I've done fine with how the goldberg plugin, I'm sure there are other ways of pulling off the same functionality.
也许您真的不想更改title元素标记的内容。也许您真的需要某种形式的面包屑导航,以便您的用户始终知道他们在您网站的导航层次结构中所处的位置。虽然我在 Goldberg 插件方面做得很好,但我确信还有其他方法可以实现相同的功能。
回答by sent-hil
I use nifty_generator's "nifty_layout" which provides with a title variable which I can call then on the page using:
我使用 nifty_generator 的“nifty_layout”,它提供了一个标题变量,然后我可以使用以下方法在页面上调用它:
<% title "Title of page" %>
<% title "Title of page" %>
I can also user <% title "Title of page", false %>to have the title just show in browser title and not in the page itself.
我也<% title "Title of page", false %>可以让标题只显示在浏览器标题中,而不是显示在页面本身中。
回答by JasonOng
You can also set it in a before_filter in your controller.
您还可以在控制器的 before_filter 中设置它。
# foo_controller.rb
class FooController < ApplicationController
before_filter :set_title
private
def set_title
@page_title = "Foo Page"
end
end
# application.html.erb
<h1><%= page_title %></h1>
You can then set conditions in the set_titlemethod to set a different titles for different actions in the controller. It's nice to be able to see all the relevant page titles within your controller.
然后,您可以在set_title方法中设置条件,为控制器中的不同操作设置不同的标题。很高兴能够在您的控制器中看到所有相关的页面标题。
回答by Henrik N
I use this pluginthese Rails helpers I wrote: http://web.archive.org/web/20130117090719/http://henrik.nyh.se/2007/11/my-rails-title-helpers/
我用 这个插件我写的这些 Rails 助手:http://web.archive.org/web/20130117090719/http: //henrik.nyh.se/2007/11/my-rails-title-helpers/
回答by Kofi Asare
approach for page titling using content_for method and a partial
使用 content_for 方法和部分页面标题的方法
1 . partial name : _page_title.html.erb
1 . 部分名称:_page_title.html.erb
<%content_for :page_title do %>
<%=title%>
<%end%>
2 . Usage in application.html.erb inside title tag
2 . 在 application.html.erb 中标题标签中的用法
<title><%=yield :page_title %></title>
3 . Usage in respective *.html.erb excluding application.html.erb Just stick this in any .html.erb and supply title of the page
3 . 在各自的 *.html.erb 中使用,不包括 application.html.erb 只需将其粘贴在任何 .html.erb 中并提供页面标题
e.g : inside index.html.erb
<%=render '_page_title', title: 'title_of_page'%>

