Module: PageTitleHelper

Defined in:
app/helpers/page_title_helper.rb

Instance Method Summary collapse

Instance Method Details

#page_title(new_title = nil, options = {}) ⇒ String

Sets or retrieves the current page title.

  • When given no argument, this will return the currently set page title or the site-wide default.

  • When given a string argument, that will be used in the formatted page title (for example with a site-wide suffix).

  • When given an ActiveModel-compliant object, its singular and plural human names will be interpolated into the model-based title template, along with the record’s ID.

You can provide templates for page titles using ‘I18n` in three broad categories: the `model`, `formatted` and `standard` keys. These will be used when you set a page title via a model, string or nothing at all, respectively.

You can specify these translation keys on a global default level, per controller or per controller action. The more specific key will take precedence.

Examples:

Set the page title to a string

page_title 'Welcome'

Set the page title based on a model name

page_title Post.find(1)
# example translation key: 'Editing %{singular} %{id}'

Pass along extra arguments to interpolate

page_title @post, author: @post.author.name

Show the page title

<title><%= title %></title>

Parameters:

  • String (String, #model_name)

    or ActiveModel-compliant object

Returns:

  • (String)

    page title component or formatted page title



33
34
35
36
37
38
39
40
41
42
43
# File 'app/helpers/page_title_helper.rb', line 33

def page_title(new_title = nil, options = {})
  if new_title
    title_from_string_or_record_or_class(new_title, options).tap do |str|
      content_for :page_title, str
    end
  elsif content_for? :page_title
    formatted_title
  else
    standard_title
  end
end