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.
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.
Terminal
rails new _6.0.0_app name-d mysql
cd app name
bundle install
rails g model task
rails g model start_time
rails g model stop_time
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
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
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.
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
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.
It feels like it's made by force, so if you have a better way, please comment!
Recommended Posts