A common approach to changing page titles dynamically: when users navigate to the top page, show only the domain name; when they go to the login page, show something like “login | domain name”.
For detailed explanations, refer to the Rails Tutorial.
You could write the title directly in application.html.erb
, but it’s better to define it in a helper for easier fine-tuning later.
# app/helpers/application_helper.rb
module ApplicationHelper
# Returns the full title for each page
def full_title(page_title = '')
base_title = "shou2017.com"
if page_title.empty?
base_title
else
page_title + " | " + base_title
end
end
end
Then, make the full_title
helper available in application.html.erb
.
<!-- app/views/layouts/application.html.erb -->
<!DOCTYPE html>
<html>
<head>
<title><%= full_title(yield(:title)) %></title>
Use provide
to set individual page titles.
<% provide(:title, " Login") %>
<h1>Login</h1>