2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

datetime_selectで年月日が「2000-01-01」になってしまった件

2
Last updated at Posted at 2019-12-07

datetime_selectで年月日+時刻を扱ったけれど、
年月日が「2000-01-01」になってしまった件。
自分の覚書として残します。
 

バージョン

Ruby:2.6.3
Rails:5.2.3

状況

Scheduleモデルにdeadlineカラムを持たせただけ。

views/schedules/_form.html.erb
<%= form_with(model: schedule, local: true) do |form| %>
  <div class="field">
    <%= form.label :deadline %>
    <%= form.datetime_select :deadline %>
  </div>

  <div class="actions">
    <%= form.submit %>
  </div>
<% end %>

こんな入力画面。
スクリーンショット 2019-12-07 18.56.49.png

2019年12月25日 18:00で設定し、[Create Schedule]をクリック。
するとこうなる。
スクリーンショット 2019-12-07 18.57.34.png

原因

deadlineカラムがtime型だった orz

db/schema.rb
  create_table "schedules", force: :cascade do |t|
    t.time "deadline"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

datetime型に直して解決!
スクリーンショット 2019-12-07 18.56.07.png

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?