[RAILS] The l method that even first graders who are not learning English can write

What is the l method?

A method used to represent the time and date. It can correspond to the specified local time. It is used when "posted at what time on what day of the month" on SNS. It is used when you want to display the created_at column of the table.

How to specify local time

Step 1

config/application.rb


class Application < Rails::Application
  config.i18n.default_locale = :ja #in Japan's case
  config.time_zone = 'Tokyo' #In the case of Tokyo
end

Make the above description in all Rails environment settings files.

Step 2

Create a ja.yml file.

config/locales/ja.yml


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

l How to use the method

After making the above two settings, just put "l" before the method to display the column with the time. ex) If the extension is .html.erb

<%= l name.created_at %> #2020/9/27/ 23:32:09 is displayed

point

Finally

It seems to be an indispensable method when implementing the posting function.

Recommended Posts

The l method that even first graders who are not learning English can write
Setting method that the size does not change even if CSS is changed