[Ruby on Rails] Change the update date and creation date to your favorite notation

Target

Not the default notation September 2020 ◯◯ day and 2020/09/◯◯ Change to 11:05 or 11:05

Development environment

ruby 2.5.7 Rails 5.2.4.3 OS: macOS Catalina

flow

Introduced 1 gem'rails-i18n' 2 Edit config / application.rb 3 Change view

Introduced gem'rails-i18n'

Gemfile


gem 'rails-i18n'

Terminal


$ bundle install

Edit config / application.rb

Add the following 4 lines to config.load_defaults.

config/application.rb


  class Application < Rails::Application
    # Initialize configuration defaults for originally generated Rails version.
    config.load_defaults 5.2
    config.paths.add 'lib', eager_load: true
    config.time_zone = 'Tokyo'
    config.i18n.default_locale = :ja
    config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]

    # 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

Change view

It is OK if you write as follows.

erb:app/views/posts/show.html.erb


<%= @post.updated_at.strftime("%Y year%m month%d day").to_s %>
<%= @post.updated_at.strftime("%Y/%m/%d").to_s %>
<%= @post.created_at.strftime("%H o'clock%M minutes").to_s %>
<%= @post.created_at.strftime("%H:%M").to_s %>

Supplement

If you use gem'rails-i18n', you can also write the error in Japanese. For more information here

Recommended Posts

[Ruby on Rails] Change the update date and creation date to your favorite notation
Change date and time to Japanese notation in Rails
[Ruby on Rails] How to change the column name
Things to remember and concepts in the Ruby on Rails tutorial
[Ruby on Rails] How to log in with only your name and password using the gem devise
Method summary to update multiple columns [Ruby on Rails]
[Ruby on Rails] Change URL id to column name
[Ruby on Rails] From MySQL construction to database change
(Ruby on Rails6) How to create models and tables
Portfolio creation Ruby on Rails
[Rails / Routing] Deepen on how to set Prefix and URI to your favorite values and resources
Deploy to Ruby on Rails Elastic beanstalk (IAM permission change)
How to run React and Rails on the same server
[Ruby On Rails] How to search and save the data of the parent table from the child table
[Ruby On Rails] How to update the calculated result to an integer type column using update_column
How to use Ruby on Rails
[Ruby on Rails] Confirmation page creation
[Ruby on Rails] Change the save destination of gem refile * Note
[Rails] How to introduce kaminari with Slim and change the design
[Ruby on Rails] Use the resources method to automatically create routes.
(Ruby on Rails6) Create a function to edit the posted content
[Ruby on Rails] Pass the parameters divided by date_select to FormObject.
Change from SQLite3 to PostgreSQL in a new Ruby on Rails project
[Ruby on Rails] How to use CarrierWave
Ruby on Rails model creation / deletion command
part of the syntax of ruby ​​on rails
Deploy to Heroku [Ruby on Rails] Beginner
How to change the timezone on Ubuntu
Ruby on Rails DB Tips for creating methods to reduce the load
Preparing to introduce jQuery to Ruby on Rails
Ruby on Rails application new creation command
Do your best with gRPC with ruby ​​on the server and PHP on the client
[Ruby on Rails] Japanese notation of errors
Environment construction of Ruby on Rails from 0 [Cloud9] (From Ruby version change to Rails installation)
(2021) Ruby on Rails administrator (admin) login creation
[Ruby on Rails] How to use redirect_to
How to solve the local environment construction of Ruby on Rails (MAC)!
What to do if you can't bundle update and bundle install after installing Ruby 3.0.0 in the Rails tutorial
[Ruby on Rails] How to use kaminari
[Rails] How to create a table, add a column, and change the column type
How to debug the processing in the Ruby on Rails model only on the console
Ruby on Rails ✕ Docker ✕ MySQL Introducing Docker and docker-compose to apps under development
[Ruby on Rails] Button to return to top
[Ruby On Rails] How to search the contents of params using include?
If you want to step up Ruby → Udemy "Introduction to Ruby on Rails for the first time-Learn Ruby and Rails from the basics and publish your web application online"
[Ruby on Rails] How to make the link destination part of the specified id
How to resolve errors that occur in the "Ruby on Rails" integration test
[Ruby on Rails] Rails tutorial Chapter 14 Summary of how to implement the status feed
[Ruby on Rails] How to Japaneseize the error message of Form object (ActiveModel)
[Beginner Procedure Manual 2] Ruby on Rails: Rails template creation
[Ruby on Rails] How to display error messages
[Ruby on Rails] Until the introduction of RSpec
How to add / remove Ruby on Rails columns
When the Ruby on Rails terminal rolls back
Publish the app made with ruby on rails
Ruby on Rails ~ Basics of MVC and Router ~
Incorporate Docker into your Ruby on Rails portfolio!
Determine the current page with Ruby on Rails
[Ruby on Rails] How to install Bootstrap in Rails
[Ruby on Rails] How to use session method
[Ruby on Rails] Post editing function (update, delete)