[RUBY] [Rails] Save start time and end time

Save start time and end time

In rails, create a program that saves the start time and end time of the task. Since there are many parts of my own style, I would appreciate it if you could refer to it as one of the methods.

画面収録 2020-09-25 2.57.23.mov.gif

Way of thinking

We aim for a mechanism that allows you to press the start and end forms only once. Specifically, write an if statement in the view so that the start button is not displayed when the task start time data is included. The process that led to this idea is as follows.

-Associate the start time (StartTime) and end time (StopTime) with the task (Task). → Problem: I can start and finish as many times as I want. -When the StartTime data is entered (when it is starting), the button is not displayed. ・ Stop Time is the same as above.

Preparation

Terminal

rails new  _6.0.0_app name-d mysql
cd app name
bundle install

Creating a model

rails g model task
rails g model start_time
rails g model stop_time

Model modification

If you make a mistake, please point it out.

php;task.rb


class Task < ApplicationRecord
  has_one :start_time
  has_one :stop_time
end

php;stop_time.rb


class StopTime < ApplicationRecord
  belongs_to :task
end

php;start_time.rb


class StartTime < ApplicationRecord
  belongs_to :task
end

Migration file

class CreateTasks < ActiveRecord::Migration[6.0]
  def change
    create_table :tasks do |t|
      t.string :title
      t.timestamps
    end
  end
end

php;202..._create_start_times.rb


class CreateStartTimes < ActiveRecord::Migration[6.0]
  def change
    create_table :start_times do |t|
      t.references :task, foreign_key: true
      t.timestamps
    end
  end
end

php;202..._create_stop_times.rb


class CreateStopTimes < ActiveRecord::Migration[6.0]
  def change
    create_table :stop_times do |t|
      t.references :task, foreign_key: true
      t.timestamps
    end
  end
end

Terminal

rails db:create
rails db:migrate

routing

Rails.application.routes.draw do
  root "tasks#index"
  resources :tasks, only: [:index, :create] do
    resources :start_times, only: :create
    resources :stop_times, only: :create
  end 
end

Only the ones to be used are specified. Also, this time it is nested, but you can actually make it without it.

controller

Terminal

rails g controller tasks
rails g controller start_times
rails g controller stop_times

php;start_times_controller.rb


class StartTimesController < ApplicationController
  def create
    StartTime.create(start_time_params)
    redirect_to root_path
  end

  private
  def start_time_params
    params.permit(:task_id)
  end
end

php;stop_times_controller.rb


class StopTimesController < ApplicationController
  def create
    StopTime.create(stop_time_params)
    redirect_to root_path
  end

  private
  def stop_time_params
    params.permit(:task_id)
  end
end

php;tasks_controller.rb


class TasksController < ApplicationController
  def index
    @tasks = Task.all
  end

  def create
    task = Task.create(task_params)
    redirect_to root_path
  end

  private
  def task_params
    params.permit(:title)
  end
end

View

Please check the url on rails routes and enter it.

php;views/tasks/index.html.haml


-#Task save form
= form_with url: tasks_path, method: :post do |f|
  = f.text_field :title
  = f.submit "send"

-#Task list display
- @tasks.each do |task|
  %br
title
  = task.title

  -#If you want to check the time, please describe it.
  - if task.start_time != nil
    %br
Start time
    = task.start_time.created_at
    %br
  - if task.stop_time != nil
End time
    = task.stop_time.created_at
    %br
    %br
  -#So far

  - if task.start_time == nil
    = form_with url: task_start_times_path(task_id: task.id), method: :post do |f|
      = f.submit "start"
      %br

  - if task.start_time != nil && task.stop_time == nil 
    = form_with url: task_stop_times_path(task_id: task.id), method: :post do |f|
      = f.submit "End"
      %br

This time, I haven't created new.html.haml at all. However, in form_with, you can specify the url corresponding to the create action and add method:: post to save it directly. If it doesn't work, you should use binding.pry etc. to check the value you are sending and the value you are receiving.

Summary

It feels like it's made by force, so if you have a better way, please comment!

Recommended Posts

[Rails] Save start time and end time
[Rails] Validate the start time (datetime type) and end time
Rails and FormData
Date and time
Start Rails Tutorial
[Rails] Difference between create method and new + save method
Rails valid? And invalid?
Rails CRUD function implementation ② (edited and detailed this time)
Change date and time to Japanese notation in Rails
[Rails] Precautions when comparing date and time with DateTime
Rails server doesn't start.
Implementation policy to dynamically save and display Timezone in Rails
[Rails] Search method when date and time have each column
AWS and on-premise time lag
[Rails] Save images using carrierwave
[Rails] Get access_token at the time of Twitter authentication with Sorcery and save it in DB
[Rails] N + 1 problems and countermeasures
Rails: Difference between resources and resources
Rails Posts and User Linkage
[Rails] require method and permit method
Rails "render method" and "redirect method"
Rails Tutorial Records and Memorandum # 0
rails path and url methods
Save and display multiple images
Rails is difficult and painful!
Introducing Bootstrap and Font-Awesome (Rails)
Rails is difficult and painful! Ⅱ
[Rails] strftime this and that
Rails web server and application server
[Rails] Why is it render if save is successful and redirect_to fails?