How to make a time chronometer from a set date?

Asked

Viewed 201 times

0

I made a small system that records tasks. The user logs in and registers a title with a description and date of type datetime in the database. In the system it can view its tasks, delete and edit.

I would like to add a type of timer that starts counting from the date and time set in the creation of the task, ie a timer that shows the time that the specific task is consuming.

I installed Gem Rails timeago and in one of my views I gave the command:

<% = Timeago_tag Time.zone.now,: nojs => true,: limit => 10.days.ago%>

With this I was able to show a timer on the screen that counts the time the page was loaded, but I would like to show the time the task is consuming.

I’m putting in my schematic code and my controller:

Schema:

ActiveRecord::Schema.define(version: 20170412011539) do

  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"

  create_table "tarefas", force: :cascade do |t|
    t.string   "titulo"
    t.text     "descricao"
    t.datetime "data"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.integer  "user_id"
    t.index ["user_id"], name: "index_tarefas_on_user_id", using: :btree
  end

  create_table "users", force: :cascade do |t|
    t.string   "email",                  default: "", null: false
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.datetime "created_at",                          null: false
    t.datetime "updated_at",                          null: false
    t.index ["email"], name: "index_users_on_email", unique: true, using: :btree
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
  end

  add_foreign_key "tarefas", "users"
end

Task controller:

class TarefasController < ApplicationController

    before_filter :authenticate_user!

    def index
        @tarefa = current_user.tarefas.all
    end

    def show
        @tarefa = Tarefa.find(params[:id])
    end

    def new
        @tarefa = Tarefa.new
    end

    def edit
        @tarefa = current_user.tarefas.find_by(id: params[:id])
    end

    def create
         @tarefa = current_user.tarefas.new(tarefa_params)
         if @tarefa.save
            redirect_to @tarefa
         else
            render 'new'
         end
    end

    def update
        @tarefa = current_user.tarefas.find_by(id: params[:id])
        if @tarefa.update(tarefa_params)
            redirect_to @tarefa
        else
            render 'edit'
        end
    end

    def destroy
        @tarefa = current_user.tarefas.find_by(id: params[:id])
        @tarefa.destroy

        redirect_to tarefas_path
    end


    private
      def tarefa_params
        params.require(:tarefa).permit(:titulo, :descricao, :data)
      end
end
  • To know the difference between two dates in days you can use (Time.now.day - Task.first.created_at.day) If you replace . day by . hour, it will appear the difference in hours... ?

  • Not to mention that if it is in real time, you will have to use a little javascript (ajax) ai.

  • Thanks. I already got it, I’m using javascript (ajax) to improve the application!

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.