前提・実現したいこと
railsの投稿時間をローカル時間で表示させたいです。
今はpost作成時に投稿時間(created_at)をUTC時間で保存し、<%= @post.created_at %>
で表示させる形にしています(現在はUTCで表示)。
DBにUTCで保存した日時をviewで表示させるときにユーザーのタイムゾーンに変換する方法ってありますか?
Railsでタイムゾーンを扱う方法を現役エンジニアが解説【初心者向け】
Railsにおけるタイムゾーンの注意点
にあるようにRailsでは東京など特定の都市のタイムゾーンに変換して表示させたり、サーバーのタイムゾーンで表示させることはできるようなのですが、各ユーザーのタイムゾーンでの表示の方法が見つかりません。
local_timeというそれっぽいgemを見つけて試したのですが、エラーになってしまいました。
basecamp/local_time
gem 'local_time' インストール local-time.js 作成、以下追加 import LocalTime from "local-time" LocalTime.start() application.js に以下を追加 //= require local-time Show.html.erb の時間表示部分を変更 <%= @post.created_at %> -> <%= local_time(@post.created_at) %>
以下がエラーログになります。
Started GET "/posts/index" for 172.18.0.1 at 2020-09-22 01:04:24 +0000 Cannot render console from 172.18.0.1! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 Processing by PostsController#index as HTML [1m [36mUser Load (1.2ms) [0m [1m [34m SELECT `users`. * FROM `users` WHERE `users`.`id` = 10 LIMIT 1 [0m ↳ app/controllers/application_controller.rb:6 Rendering posts/index.html.erb within layouts/application [1m [36mPost Load (4.0ms) [0m [1m [34m SELECT `posts`. * FROM `posts` ORDER BY `posts`.`created_at` DESC [0m ↳ app/views/posts/index.html.erb:20 [1m [36mCACHE User Load (0.2ms) [0m [1m [34mSELECT `users`.* FROM `users` WHERE `users`.`id` = 10 LIMIT 1 [0m [["id", 10], ["LIMIT", 1]] ↳ app/views/posts/index.html.erb:23 [1m [36mCACHE User Load (0.5ms) [0m [1m [34mSELECT `users`.* FROM `users` WHERE `users`.`id` = 10 LIMIT 1 [0m [["id", 10], ["LIMIT", 1]] ↳ app/views/posts/index.html.erb:23 [1m [36mUser Load (1.7ms) [0m [1m [34m SELECT `users`. * FROM `users` WHERE `users`.`id` = 1 LIMIT 1 [0m ↳ app/views/posts/index.html.erb:23 [1m [36mUser Load (1.8ms) [0m [1m [34m SELECT `users`. * FROM `users` WHERE `users`.`id` = 2 LIMIT 1 [0m ↳ app/views/posts/index.html.erb:23 [1m [36mCACHE User Load (0.4ms) [0m [1m [34mSELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1 [0m [["id", 1], ["LIMIT", 1]] ↳ app/views/posts/index.html.erb:23 Rendered posts/index.html.erb within layouts/application (108.4ms) Completed 200 OK in 2193ms (Views: 2115.2ms | ActiveRecord: 9.7ms) [1m [35m (3.2ms) [0m [1m [35m SET NAMES utf8 ,@@SESSION.sql_mode = CONCAT( CONCAT( @@sql_mode ,',STRICT_ALL_TABLES' ) ,',NO_AUTO_VALUE_ON_ZERO' ) ,@@SESSION.sql_auto_is_null = 0 ,@@SESSION.wait_timeout = 2147483 [0m [1m [36mPost Load (100.1ms) [0m [1m [34m SELECT `posts`. * FROM `posts` LIMIT 11 [0m Started GET "/posts/7" for 172.18.0.1 at 2020-09-22 01:11:20 +0000 Cannot render console from 172.18.0.1! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 [1m [35m (1.6ms) [0m [1m [35m SET NAMES utf8 ,@@SESSION.sql_mode = CONCAT( CONCAT( @@sql_mode ,',STRICT_ALL_TABLES' ) ,',NO_AUTO_VALUE_ON_ZERO' ) ,@@SESSION.sql_auto_is_null = 0 ,@@SESSION.wait_timeout = 2147483 [0m ↳ /usr/local/bundle/gems/activerecord-5.2.4.4/lib/active_record/log_subscriber.rb:98 Processing by PostsController#show as HTML Parameters: {"id"=>"7"} [1m [36mUser Load (3.4ms) [0m [1m [34m SELECT `users`. * FROM `users` WHERE `users`.`id` = 10 LIMIT 1 [0m ↳ app/controllers/application_controller.rb:6 [1m [36mPost Load (1.5ms) [0m [1m [34m SELECT `posts`. * FROM `posts` WHERE `posts`.`id` = 7 LIMIT 1 [0m ↳ app/controllers/posts_controller.rb:15 [1m [36mCACHE User Load (0.0ms) [0m [1m [34mSELECT `users`.* FROM `users` WHERE `users`.`id` = 10 LIMIT 1 [0m [["id", 10], ["LIMIT", 1]] ↳ app/controllers/posts_controller.rb:16 [1m [35m (4.0ms) [0m [1m [34m SELECT COUNT( * ) FROM `likes` WHERE `likes`.`post_id` = 7 [0m ↳ app/controllers/posts_controller.rb:17 Rendering posts/show.html.erb within layouts/application Rendered posts/show.html.erb within layouts/application (314.5ms) Completed 500 Internal Server Error in 717ms (ActiveRecord: 8.9ms) ActionView::Template::Error (undefined method `local_time' for #<#<Class:0x00007f4ac5584118>:0x000055c5ae073180>): 22: <br/> 23: <div class="post-time"> 24: <%= @post.created_at %> 25: <%= local_time(@post.created_at) %> 26: </div> 27: <% if Like.find_by(user_id: @current_user.id, post_id: @post.id) %> 28: <%= link_to("/likes/#{@post.id}/destroy", {method: "post"}) do %> app/views/posts/show.html.erb:25:in `_app_views_posts_show_html_erb__3079554789774239632_47153758291420'
gem local_timeでなくても、投稿時間をローカル時間で表示させる方法がありましたら教えてください。
よろしくお願いします。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。