{"id":10955,"date":"2024-07-19T07:15:55","date_gmt":"2024-07-19T07:15:55","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=10955"},"modified":"2024-07-19T07:15:55","modified_gmt":"2024-07-19T07:15:55","slug":"display-date-variables-on-ruby-on-rails-site","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/ruby-on-rails\/display-date-variables-on-ruby-on-rails-site","title":{"rendered":"How to Display Date Variables On Site Using Ruby on Rails?"},"content":{"rendered":"<p>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.<\/p>\n<p>Here are the steps to resolve the issue:<\/p>\n<p><strong>Update the Migration File:<\/strong> Change the data type of valid_from and valid_until columns to datetime.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"ruby\">\r\nclass CreatePasses &lt; ActiveRecord::Migration[7.0]\r\n  def change\r\n    create_table :passes do |t|\r\n      t.datetime :valid_from\r\n      t.datetime :valid_until\r\n      t.boolean :is_time_limited\r\n      t.integer :entries_left\r\n      t.string :name\r\n\r\n      t.timestamps\r\n    end\r\n  end\r\nend\r\n<\/pre>\n<p>&nbsp;<br \/>\n<strong>Generate a New Migration to Change Column Types:<\/strong> If you already have the passes table created and data in it, you need to create a new migration to change the column types.<\/p>\n<p>rails generate migration ChangeValidFromAndValidUntilToDatetimeInPasses<\/p>\n<p>Then, update the generated migration file:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"ruby\">\r\nclass ChangeValidFromAndValidUntilToDatetimeInPasses < ActiveRecord::Migration[7.0]\r\n  def change\r\n    change_column :passes, :valid_from, :datetime\r\n    change_column :passes, :valid_until, :datetime\r\n  end\r\nend\r\n<\/pre>\n<p><strong> Run the migration:<\/strong> rails db:migrate<\/p>\n<p><strong>Update the Form View:<\/strong> Update the form fields to use datetime_local_field to allow input for both date and time.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"ruby\">\r\n<%= form_with model: @pass do |form| %>\r\n  <div>\r\n    <%= form.label :name %><br>\r\n    <%= form.text_field :name %>\r\n    <% @pass.errors.full_messages_for(:name).each do |message| %>\r\n      <div><%= message %><\/div>\r\n    <% end %>\r\n  <\/div>\r\n  <div>\r\n    <%= form.label :valid_from %><br>\r\n    <%= form.datetime_local_field :valid_from %>\r\n    <% @pass.errors.full_messages_for(:valid_from).each do |message| %>\r\n      <div><%= message %><\/div>\r\n    <% end %>\r\n  <\/div>\r\n  <div>\r\n    <%= form.label :valid_until %><br>\r\n    <%= form.datetime_local_field :valid_until %>\r\n    <% @pass.errors.full_messages_for(:valid_until).each do |message| %>\r\n      <div><%= message %><\/div>\r\n    <% end %>\r\n  <\/div>\r\n  <div>\r\n    <%= form.submit %>\r\n  <\/div>\r\n<% end %>\r\n<\/pre>\n<p><strong>Display Datetime in the Show View:<\/strong> Ensure that the valid_from and valid_until attributes are displayed in the desired format in the show view.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"ruby\">\r\n<h1><%= @pass.name %><\/h1>\r\n<p><%= @pass.valid_from.strftime(\"%Y-%m-%d %H:%M:%S\") %><\/p>\r\n<p><%= @pass.valid_until.strftime(\"%Y-%m-%d %H:%M:%S\") %><\/p>\r\n<\/pre>\n<p>By following these steps, you should be able to display the datetime information correctly and have appropriate input fields in your forms.<br \/>\n(*Note: Adjust the code as needed.)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":10956,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[11],"tags":[],"class_list":["post-10955","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ruby-on-rails"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/10955"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=10955"}],"version-history":[{"count":1,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/10955\/revisions"}],"predecessor-version":[{"id":10957,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/10955\/revisions\/10957"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/10956"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=10955"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=10955"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=10955"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}