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


Ace the Technical Interview

  • Easily find the gaps in your knowledge
  • Get customized lessons based on where you are
  • Take consistent action everyday
  • Builtin accountability to keep you on track
  • You will solve bigger problems over time
  • Get the job of your dreams

Take the 30 Day Coding Skills Challenge

Gain confidence to attend the interview

No spam ever. Unsubscribe anytime.