[Rails] How to change the page title of the browser for each page

Target

スクリーンショット 2020-06-27 10.43.42.png

Development environment

・ Ruby: 2.5.7 Rails: 5.2.4 ・ Vagrant: 2.2.7 -VirtualBox: 6.1 ・ OS: macOS Catalina

Premise

The following has been implemented.

Slim introduction

Implementation

1. Edit ʻapplication_helper.rb`

application_helper.rb


module ApplicationHelper
  #Postscript
  def full_title(page_title = '')
    base_title = "Bookers"
    if page_title.empty?
      base_title
    else
      "#{ page_title } | #{ base_title }"
    end
  end
end

[Explanation]

** ◎ Set the base title (app name, etc.) and assign it to a variable. ** **

base_title = "Bookers"

** ◎ If the title of each page received as an argument is empty, only the base title is displayed. ** **

if page_title.empty?
  base_title

** ◎ If the title of each page received as an argument exists, both titles are displayed. ** **

else
  "#{ page_title } | #{ base_title }"

If page_title is" book list ", it will be displayed as book list | Bookers.

2. Edit the view

① Edit ʻapplication.html.slim`.

slim:application.html.slim


/Change before
title
  | Bookers

/After change
title
  = full_title(yield(:title))

② Edit the view of each page.

As an example, set the title of books / index.html.slim.

slim:books/index.html.slim


/Postscript
= provide(:title, 'Book list')

Recommended Posts

[Rails] How to change the page title of the browser for each page
How to switch the display of the header menu for each transition page
[Rails] How to change the column name of the table
[Rails] Button to return to the top of the page
[Ruby on Rails] How to change the column name
How to display the amount of disk used by Docker container for each container
[Rails] How to get the contents of strong parameters
[Ruby on Rails] Quickly display the page title in the browser
How to change the setting value of Springboot Hikari CP
[Rails] How to display the list of posts by category
How to change the contents of the jar file without decompressing
[Swift] How to dynamically change the height of the toolbar on the keyboard
[Rails] How to get the URL of the transition source and redirect
[Rails] How to introduce kaminari with Slim and change the design
How to get the contents of Map using for statement Memorandum
[Rails] How to omit the display of the character string of the link_to method
[chown] How to change the owner of a file or directory
How to check for the contents of a java fixed-length string
[Rails 5] How to display the password change screen when using devise
How to change app name in rails
Change the default timezone for the rails app
[Rails] How to use the map method
How to determine the number of parallels
[Rails] Change the label name of f.label
How to sort the List of SelectItem
How to decorate the radio button of rails6 form_with (helper) with CSS
How to change the maximum and maximum number of POST data in Spark
How to solve the local environment construction of Ruby on Rails (MAC)!
How to change the value of a variable at a breakpoint in intelliJ
[Rails] How to create a table, add a column, and change the column type
[For Rails beginners] Summary of how to use RSpec (get an overview)
[Rails] Articles for beginners to organize and understand the flow of form_with
How to make the schema of the URL generated by Rails URL helper https
[swift5] How to change the color of TabBar or the color of item of TabBar with code
[Rails] How to solve the time lag of created_at after save method
Change the save destination of the image to S3 in the Rails app. Part 2
[Rails] How to decide the destination by "rails routes"
How to find the cause of the Ruby error
Customize how to divide the contents of Recyclerview
[Rails] How to implement unit tests for models
How to get today's day of the week
[For beginners] How to implement the delete function
Change the timezone of the https-portal container to JST
Output of how to use the slice method
[Java] (for MacOS) How to set the classpath
How to use JQuery in js.erb of Rails6
How Microservices Change the Way of Developing Applications
How to display the result of form input
How to specify index of JavaScript for statement
How to check Rails commands in the terminal
[Ruby on Rails] How to make the link destination part of the specified id
[Swift] How to change the order of Bar Items in Tab Bar Controller [Beginner]
[Rails5.2] Support for emoji of Mysql 5.7 in Docker (change character code to utf8mb4)
How to make a unique combination of data in the rails intermediate table
[Ruby on Rails] How to Japaneseize the error message of Form object (ActiveModel)
[Java] How to get the URL of the transition source
How to delete / update the list field of OneToMany
How to set the display time to Japan time in Rails
[rails] After option useful when you want to change the order of DB columns
How to install Docker in the local environment of an existing Rails application [Rails 6 / MySQL 8]
How to write Rails