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,
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!
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!
By using the l method, I was able to easily correct the posting time to Japan time through the preset Japan time format.
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