I summarized the flow until implementing simple_calendar in Ruby on Rails.

I wanted to implement a reservation time setting function using a calendar in the original application and summarized the flow.

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)

** What you want to do ** Image from Gyazo

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

① Write the following in the Gemfile and bundle install.

   gem "simple_calendar", "~> 2.0"

② Rails s will be restarted.

③ Create a model. Since this is a reservation function, we will create a reservation model.

  % rails g model reservation

④ Create a migration file.

   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

⑤ Create an association in the model file. I also write it in the user model doctor model (omitted)

   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

⑥ Next, you need a controller, but there are some caveats.

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

⑦ By doing this, you can separate the controller that the user passes through and the controller that the doctor passes through.

doctorreservation.png

user.reservation.png

⑧ Set the routing. I want to use the namespace because I want to use the specified path for each devise.

   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

⑨ Create the required view.

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.

⑩ Define reservation creation, edit, and delete methods in the doctor controller, and make reservation decisions in the user controller.

`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

I summarized the flow until implementing simple_calendar in Ruby on Rails.
[Ruby on Rails] Until the introduction of RSpec
Rails new in Ruby on Rails ~ Memorandum until deployment 2
Rails new in Ruby on Rails ~ Memorandum until deployment 1
[Ruby on Rails Tutorial] Error in the test in Chapter 3
[Ruby on Rails] Quickly display the page title in the browser
Beginner Ruby on Rails What I learned is being summarized
Ruby on Rails Japanese-English support i18n
[Ruby on Rails] Stop "looping until ..."
[Ruby on Rails] I want to get the URL of the image saved in Active Storage
Where I was interested in Progate's Ruby on Rails course [params]
Things to remember and concepts in the Ruby on Rails tutorial
part of the syntax of ruby ​​on rails
Ruby on Rails in Visual Studio Codespaces
Beginners create portfolio in Ruby on Rails
"" Application.js "is not present in the asset pipeline" error in Ruby on Rails
How to debug the processing in the Ruby on Rails model only on the console
Cloud IDE: Heroku couldn't be installed in the Ruby on Rails tutorial
A series of flow of table creation → record creation, deletion → table deletion in Ruby on Rails
I tried to organize the session in Rails
When the Ruby on Rails terminal rolls back
Recommendation of Service class in Ruby on Rails
Publish the app made with ruby on rails
I summarized the naming conventions for each Rails
(Ruby on Rails6) Creating data in a table
[Ruby on Rails] How to install Bootstrap in Rails
I want to get the value in Ruby
I made a portfolio with Ruby On Rails
[Ruby on Rails] Solving the addiction when setting crontab using whenever in EC2
How to resolve errors that occur in the "Ruby on Rails" integration test
[Ruby on Rails] How to write enum in Japanese
Docker the development environment of Ruby on Rails project
[Ruby On Rails] In the nested state, the description in parentheses written after Prefix in the link_to method
[Ruby on Rails] How to change the column name
[Ruby] I can't install puma on Mac [Rails, etc.]
I summarized the display format of the JSON response of Rails
[Ruby On Rails] How to reset DB in Heroku
[Ruby / Rails] Set a unique (unique) value in the class
Ruby on Rails Elementary
Ruby on Rails basics
[Ruby on Rails] Post image preview function in refile
Solve the N + 1 problem with Ruby on Rails: acts-as-taggable-on
[Rilas] What I learned in implementing the pagination function.
Questions about implementing the Like feature (Ajax) in Rails
Ruby On Rails Association
(Ruby on Rails6) Reflecting the posted content from the form
Try using the query attribute of Ruby on Rails
What I was interested in in Progate's Ruby on Rails course [Each statement of error message]
Ruby on Rails 5 quick learning practice guide that can be used in the field Summary
[Ruby on Rails] credential does not work in production environment even though I saved production.key
[Ruby on Rails] Only the user who posted can edit
I tried installing Ruby on Rails related plugin with vim-plug
Difficulties in building a Ruby on Rails environment (Windows 10) (SQLite3)
(Ruby on Rails6) Display of the database that got the id of the database
[Environment construction] Get the Ruby on Rails 6 development environment within 1 hour
Delete all the contents of the list page [Ruby on Rails]
A note about the seed function of Ruby on Rails
[Ruby on Rails] Automatically enter the address from the zip code
What I did in the version upgrade from Ruby 2.5.2 to 2.7.1
[Apple login] Sign in with Apple implementation procedure (Ruby on Rails)
How to display a graph in Ruby on Rails (LazyHighChart)