The issue you are facing is because you are using the time data type for the valid_from and valid_until columns. The time data type only stores the time part, not the date part. To store both date and time, you should use the datetime data type instead.

Here are the steps to resolve the issue:

Update the Migration File: Change the data type of valid_from and valid_until columns to datetime.

class CreatePasses < ActiveRecord::Migration[7.0]
  def change
    create_table :passes do |t|
      t.datetime :valid_from
      t.datetime :valid_until
      t.boolean :is_time_limited
      t.integer :entries_left
      t.string :name

      t.timestamps
    end
  end
end

 
Generate a New Migration to Change Column Types: If you already have the passes table created and data in it, you need to create a new migration to change the column types.

rails generate migration ChangeValidFromAndValidUntilToDatetimeInPasses

Then, update the generated migration file:

class ChangeValidFromAndValidUntilToDatetimeInPasses < ActiveRecord::Migration[7.0]
  def change
    change_column :passes, :valid_from, :datetime
    change_column :passes, :valid_until, :datetime
  end
end

Run the migration: rails db:migrate

Update the Form View: Update the form fields to use datetime_local_field to allow input for both date and time.

<%= form_with model: @pass do |form| %>
  
<%= form.label :name %>
<%= form.text_field :name %> <% @pass.errors.full_messages_for(:name).each do |message| %>
<%= message %>
<% end %>
<%= form.label :valid_from %>
<%= form.datetime_local_field :valid_from %> <% @pass.errors.full_messages_for(:valid_from).each do |message| %>
<%= message %>
<% end %>
<%= form.label :valid_until %>
<%= form.datetime_local_field :valid_until %> <% @pass.errors.full_messages_for(:valid_until).each do |message| %>
<%= message %>
<% end %>
<%= form.submit %>
<% end %>

Display Datetime in the Show View: Ensure that the valid_from and valid_until attributes are displayed in the desired format in the show view.

<%= @pass.name %>

<%= @pass.valid_from.strftime("%Y-%m-%d %H:%M:%S") %>

<%= @pass.valid_until.strftime("%Y-%m-%d %H:%M:%S") %>

By following these steps, you should be able to display the datetime information correctly and have appropriate input fields in your forms.
(*Note: Adjust the code as needed.)

Support On Demand!

Ruby on Rails