ruby '2.6.5' rails '6.0.0' simple_calendar "~> 2.0"
`As a prerequisite, different devises are created for user and doctor. `` Below is the related URL. Routing settings from file creation when creating multiple devises and creating separate login screens [I want to change the path after new registration after logging in with multiple devises. ] (https://qiita.com/kinpin/items/57b1657f9054189956c3)
Create a function that can perform such a calendar function with user and doctor (each devise).
Create reservation, edit reservation time, delete → Function that doctor can do
Make a reservation → Function that user can do
gem "simple_calendar", "~> 2.0"
% rails g model reservation
class CreateReservations < ActiveRecord::Migration[6.0]
def change
create_table :reservations do |t|
t.references :doctor, foreign_key:true
t.references :user, foreign_key:true
t.datetime :start_time
t.datetime :end_time
t.timestamps
end
end
end
class Reservation < ApplicationRecord
belongs_to :doctor
belongs_to :user, optional: true
validates :start_time, presence: true
validates :end_time, presence: true
end
optional: true is described because it allows the foreign key nil. This is because there is no user_id when creating a new reservation. The reservation is created by the doctor, so there is always a doctor_id.
Run rails db; migrate.
% rails db:migrate
The function that user can do is different from the function that doctor can do, and it is strange that user and doctor pass through the same controller due to security issues. So create controllers under different directories.
% rails g controller doctors/reservations
% rails g controller users/reservations
namespace :doctors do
resources :reservations
end
namespace :users do
resources :reservations
end
If you do rails routes, you can see that each route has been created.
DELETE /users/:id(.:format) users#destroy
doctors_reservations GET /doctors/reservations(.:format) doctors/reservations#index
POST /doctors/reservations(.:format) doctors/reservations#create
new_doctors_reservation GET /doctors/reservations/new(.:format) doctors/reservations#new
edit_doctors_reservation GET /doctors/reservations/:id/edit(.:format) doctors/reservations#edit
doctors_reservation GET /doctors/reservations/:id(.:format) doctors/reservations#show
PATCH /doctors/reservations/:id(.:format) doctors/reservations#update
PUT /doctors/reservations/:id(.:format) doctors/reservations#update
DELETE /doctors/reservations/:id(.:format) doctors/reservations#destroy
users_reservations GET /users/reservations(.:format) users/reservations#index
POST /users/reservations(.:format) users/reservations#create
new_users_reservation GET /users/reservations/new(.:format) users/reservations#new
edit_users_reservation GET /users/reservations/:id/edit(.:format) users/reservations#edit
users_reservation GET /users/reservations/:id(.:format) users/reservations#show
PATCH /users/reservations/:id(.:format) users/reservations#update
PUT /users/reservations/:id(.:format) users/reservations#update
DELETE /users/reservations/:id(.:format) users/reservations#destroy
It is displayed on the doctor's show screen. The doctor creates a link that transitions to the new reservation screen, edit screen, and delete screen on the doctor show screen, and the user creates a link that transitions to the reservation screen on the doctor show screen.
First, insert the calendar.
<%= month_calendar do |date| %>
<%= date.day %>
<% end %>
This completes the calendar template creation.
`Here, which method is used to determine the reservation of user, but the conclusion is edit. `` Since the reservation table contains data at the time of the reservation list, adding user_id is considered to add one of the records.
doctors/reservations_controller.rb
def new
@reservation = Reservation.new
end
def create
@reservation = Reservation.new(reservation_params)
if @reservation.save
redirect_to doctor_path(current_doctor.id)
else
render :new
end
end
def edit
@reservation = Reservation.find(params[:id])
end
def update
@reservation = Reservation.find(params[:id])
if @reservation.update(reservation_params)
redirect_to doctor_path(current_doctor.id)
else
render :edit
end
end
def destroy
@reservation = Reservation.find(params[:id])
if @reservation.destroy
redirect_to doctor_path(current_doctor.id)
else
render :show
end
end
private
def reservation_params
params.require(:reservation).permit(:start_time, :end_time).merge(doctor_id: current_doctor.id)
end
end
users/reservations_controller.rb
def edit
@reservation = Reservation.find(params[:id])
@doctor = Doctor.find(params[:id])
end
def update
@reservation = Reservation.find(params[:id])
if @reservation.update(user_id: current_user.id)
redirect_to doctor_path(@reservation.doctor_id)
else
render :edit
end
end
The view display still needs to be fine-tuned, but this allows you to set a minimum calendar reservation feature. I will add it again. This is how we introduced simple_calendar. If you have any suggestions, please let us know! !!
Recommended Posts