Hey there
Code and (longer) quick-start guide can also be found here:
To customize page titles there are many different methods, among the
most popular (and best looking) is certainly:
#30 Pretty Page Title - RailsCasts, even more there
are numerous plugins on github
http://github.com/search?type=Repositories&language=rb&q=title,
so why was there the need to create yet another one?
Because we required a method to have internationalized titles and this
soon became very cumbersome und cludgy But well, a few code
examples say more than a thousand words:
in the layout
…
<%= h page_title %> ...then in the translations:
en:
contacts:
index:
title: “All contacts”
Thats it. So if /contacts/ is called and then the view contacts/
index.html.erb is rendered, the title “All contacts” is used. To
customize the title e.g. to display the contacts name in contacts/
show.html.erb do:
in contacts/show.html.erb
<%= h page_title { @contact.name } %>
It also features some options to change the format, add custom parts
to the output etc. It can be installed as a gem, plugin or git
submodule → GitHub - lwe/page_title_helper: Simple, internationalized and DRY page titles and headings for Rails. If you find any
issues or problems, I’m sure there are just drop me a line or
create a ticket on github.
Note: this plugin has only been tested and used on Rails 2.3.x.
Grüsse, Lukas
PS: For those interested: internally the method from railscasts is
used to set custom titles.
PPS: More examples and a description of all options can be found on
github
PPPS: It’s small LOC = 60, has tests (but Code to Test Ratio is only
1:1.4) and 100% code coverage.
PPPPS: Finally some bad news: it relies on some Rails internals to
e.g. obtain the first renderer etc., so if those change the plugin
might break… so if anyone knows better alternatives (check
read_first_render_path and read_page_title_content_block methods)…