[RUBY] [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] How to find the sum of each digit
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
Let's summarize how to extend the expiration date of Rails
[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 / Heroku / MySQL] How to reset the DB of Rails application on Heroku
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
How to change the timezone on Ubuntu
[Rails] Change the label name of f.label
Ransack sort_link How to change the color!
How to sort the List of SelectItem
The process of introducing Vuetify to Rails
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)
[Ruby On Rails] How to search the contents of params using include?
[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
[Rails] Introduction of pry-rails ~ How to debug binding.pry
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
I summarized the naming conventions for each Rails
[Rails] How to use Gem'rails-i18n' for Japanese support
How Microservices Change the Way of Developing Applications
How to display the result of form input
http: // localhost: How to change the port number
[Java] How to get the authority of the folder
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