Home > Software design >  How to display date variables on site using Ruby on Rails?
How to display date variables on site using Ruby on Rails?

Time:08-11

I wanted to display my time variables to see when pass has started and when will it end but don't know how and couldn't find solution. I tried something very simple but it doesn't work:
show.html.erb

<h1><%= @pass.name %></h1>
<p><%= @pass.valid_from %></p>
<p><%= @pass.valid_until %></p>

create_passes.rb migration

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

      t.timestamps
    end
  end
end

passes_controller.rb

class PassesController < ApplicationController
  def index
    @passes = Pass.all
  end

  def show
    @pass = Pass.find(params[:id])
  end

  def new
    @pass = Pass.new
  end

  def create
    @pass = Pass.new(pass_params)

    if @pass.save
      redirect_to @pass
    else
      render :new, status: :unprocessable_entity
    end
  end

  def edit
    @pass = Pass.find(params[:id])
  end

  def update
    @pass = Pass.find(params[:id])

    if @pass.update(pass_params)
      redirect_to @pass
    else
      render :edit, status: :unprocessable_entity
    end
  end

  def destroy
    @pass = Pass.find(params[:id])
    @pass.destroy

    redirect_to root_path, status: :see_other
  end

  private
    def pass_params
      params.require(:pass).permit(:name, :is_time_limited, :valid_from, :valid_until, :entries_left)
    end
end

new/edit pass form view

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

rails server log

Started GET "/passes/2" for ::1 at 2022-08-10 18:18:18  0200
Processing by PassesController#show as HTML
  Parameters: {"id"=>"2"}
   (0.1ms)  SELECT sqlite_version(*)
  ↳ app/controllers/passes_controller.rb:7:in `show'
  Pass Load (0.2ms)  SELECT "passes".* FROM "passes" WHERE "passes"."id" = ? LIMIT ?  [["id", 2], ["LIMIT", 1]]
  ↳ app/controllers/passes_controller.rb:7:in `show'
  Rendering layout layouts/application.html.erb
  Rendering passes/show.html.erb within layouts/application
  Rendered layouts/_navbar.html.erb (Duration: 1.0ms | Allocations: 724)
  Rendered passes/show.html.erb within layouts/application (Duration: 2.8ms | Allocations: 1670)
  Rendered layout layouts/application.html.erb (Duration: 6.9ms | Allocations: 4039)
Completed 200 OK in 31ms (Views: 7.5ms | ActiveRecord: 0.5ms | Allocations: 5333)

Also, is it better to use time or datatime type for dates and time?

CodePudding user response:

Ok I believe I know why you are not getting any values. You setup your DB to use a time field

 t.time :valid_from
 t.time :valid_until

But you are using a date_field in the view:

form.date_field :valid_from

You need to change this to:

form.time_field :valid_from

The difference between time or datetime is datetime includes a DATE and a TIME, where TIME ONLY has a time so it depends on what your use case is. You can also use just a date and then it would be

form.date_field :valid_from

If you need dates you will need to change your model and DB to use either t.date or t.datetime. If you don't have a lot of information in your DB you can do a rails db:rollback and modify your migration file would be the quickest and easiest way to fix it. IF you DO have data you will have to create a new migration to change the fields.

  • Related