Pretty Page Title in Rails 5

Problem : The title does not change when you view different pages on the site.

Steps

Step 1

Add the following code to application layout file.

<title> MyComm Site - <%= content_for?(:title) ? yield(:title) : 'The Biggest Online Store on Earth' %> </title>

Step 2

In products/new.html.erb use the title view helper to display the title:

<% title "New Product" %>

Step 3

The title view helper is defined in the application_helper.rb:

module ApplicationHelper
  def title(page_title)
    content_for(:title) { page_title }
  end
end

The view that does not call the title helper will now have the default title MyComm Site : The Biggest Online Store on Earth.

Summary

In this article, you learned how to use custom view helper to customize the title an any page of your Rails 5 web app.


Related Articles


Create your own user feedback survey