[Must-see for beginners] I changed the display of the posting time to Japanese time [l method]

If you pull out the information from the database of the application made with Rails and output created_at and updated_at, it is not the Japanese time. for that reason,

  1. Change the time to Japanese time in the configuration file
  2. Display the time using the l method And fix it.

1. Change the time to Japanese time in the configuration file

Before that ↓

What is a configuration file? ??

rails manages things related to the execution environment in the config directory. The application.rb file in the> config directory describes settings that are common to all Rails application development environments.

First, open the config/application.rb file and add the following settings.

config/application.rb


module "app name"
  class Application < Rails::Application
    # Initialize configuration defaults for originally generated Rails version.
    config.load_defaults 6.0

    config.i18n.default_locale = :ja   #←←←←←←←← Add this line
    config.time_zone = 'Tokyo'         #←←←←←←←← Add this line

    # Settings in config/environments/* take precedence over those specified here.
    # Application configuration can go into files in config/initializers
    # -- all .rb files in that directory are automatically loaded after loading
    # the framework and any gems in your application.
  end
end

Next, create a file called "ja.yml" in the config/locales directory and write it as follows.

config/ja.yml


ja:
  time:
    formats:
      default: "%Y/%m/%d %H:%M:%S"

This completes the time change settings!

2. Display the time using the l method

Before that ↓

What is the l method?

l is taken from the English acronym for localize. "localize" That is, it means to correspond to the local area. This method is used to represent a date or time, but it can correspond to a specified local time!

Write it in the view file as follows.

erb:app/views/reviews/index.html.erb


<h4 class="title is-2 is-centered has-text-white">Word-of-mouth communication</h4>
<% @reviews.each do |review| %>
<div class="card review_card mb-4">
  <header class="card-header">
    <div class="card-header-title">
      <%= review.user.nickname %>
    </div>
  </header>
  <div class="card-content pt-2">
    <div class="content">
      <div class="star-rating mb-4">
        <div class="star-rating-front" style="width: <%= review.score * 20 %>%">★★★★★</div>
        <div class="star-rating-back">★★★★★</div>
      </div>
      <%= simple_format(review.content) %>
      <div class="has-text-right is-italic" ><%= l review.updated_at %></div>   #←←←←←←←←←←←←←← here
    </div>
  </div>
  <% if user_signed_in? %>
    <%if current_user.id == review.user_id %>
      <footer class="card-footer">
        <%= link_to 'To edit', edit_agent_review_path(review.agent_id, review.id), method: :get, class: "card-footer-item" %>
        <%= link_to 'delete', agent_review_path(review.agent_id, review.id), method: :delete, class: "card-footer-item" %>
      </footer>
    <% else %>
      <footer class="card-footer">
        <a href="#" class="card-footer-item">To comment</a>
      </footer>
    <% end %>
  <% end %>

</div>
<% end %>

Just add "l" before the variable you want to output, like <% = l review.updated_at%>! Please restart the server properly! スクリーンショット 2020-12-24 18.33.36.png

By using the l method, I was able to easily correct the posting time to Japan time through the preset Japan time format.

Finally

How was my first Qiita post? Please feel free to let us know if there are any mistakes or confusing points! See you in the next post.

Recommended Posts

[Must-see for beginners] I changed the display of the posting time to Japanese time [l method]
Use the l method for time notation
I want to display the number of orders for today using datetime.
I want to display the name of the poster of the comment
I tried to introduce Bootstrap 4 to the Rails 6 app [for beginners]
[Rails] I tried using the button_to method for the first time
I want to expand the clickable part of the link_to method
I want to narrow down the display of docker ps
[For Swift beginners] I tried to summarize the messy layout cycle of ViewController and View
Implementation method of linking multiple images to one post and posting at the same time
The story of Collectors.groupingBy that I want to keep for posterity
I tried to explain the method
[Rails] How to omit the display of the character string of the link_to method
Must-see for beginners! Specific method to manage X code project on Github ②
Glassfish tuning list that I want to keep for the time being
The story of releasing the Android app to the Play Store for the first time.
How to switch the display of the header menu for each transition page
I want to pass the argument of Annotation and the argument of the calling method to aspect
I translated the grammar of R and Java [Updated from time to time]
[Rails] Articles for beginners to organize and understand the flow of form_with
I want you to use Enum # name () for the Key of SharedPreference
[Rails] How to solve the time lag of created_at after save method
Introduction to java for the first time # 2
I wanted to add @VisibleForTesting to the method
I was addicted to the roll method
How to display the amount of disk used by Docker container for each container
I want you to use Scala as Better Java for the time being
I changed the column name, but it said Undefined method for the column before the change.
I tried using Docker for the first time
I want to output the day of the week
Set the time of LocalDateTime to a specific time
I want to var_dump the contents of the intent
[For beginners] How to implement the delete function
Output of how to use the slice method
[Ruby] Code to display the day of the week
Android Studio development for the first time (for beginners)
I tried touching Docker for the first time
How to display the result of form input
Summary of Japan time setting and display method
[Introduction to Java] Basics of java arithmetic (for beginners)
[Rails] How to convert UC time display to Japanese time display
I tried to create a log reproduction script at the time of apt install
[Beginner's point of view] I tried to solve the FizzBuzz problem "easily" with Ruby!
[Solution] A memo that I had a hard time because the format of sinatra-validation changed
[Rails] I want to display the link destination of link_to in a separate tab
[Java] How to display the day of the week acquired by LocalDate, DateTimeformatter in Japanese
[Rails] How to display the weather forecast of the registered address in Japanese using OpenWeatherMap
I made a Docker image of SDAPS for Japanese
How to set the display time to Japan time in Rails
I want to call a method of another class
[For beginners] DI ~ The basics of DI and DI in Spring ~
I was addicted to the record of the associated model
I tried to summarize the state transition of docker
[For beginners] Quickly understand the basics of Java 8 Lambda
I made a method to ask for Premium Friday
05. I tried to stub the source of Spring Boot
I tried to reduce the capacity of Spring Boot
I want to know the answer of the rock-paper-scissors app
I summarized the display format of the JSON response of Rails
[For beginners] Minimum sample to display RecyclerView in Java
Introduction to Java for beginners Basic knowledge of Java language ①