Implement a reservation system using Rails and simple calendar! Let's add validation to datetime!

What you want to implement

Use gem's simple calendar to schedule reservations in the calendar I will proceed on the premise that I have basic knowledge of ruby and Rails

Development environment

Rails 6.0.3 ruby 2.7.0

Add reservations table

This time, assuming that you will make a mini application, create a reservations table and add columns for name (string) and start_time (datetime type).

rails new calendar_app

First of all, the usual rails new

bundle

Let's also make a habit of bundle install.

Gemfile


gem 'simple_calendar', '~> 2.0'
bundle

Add simple_calendar to gemfile and bundle install again

rails g scaffold reservationc name start_time:datetime

This time I would like to easily implement the application using scaffold.

rails db:migrate

Don't forget the usual rails db: migrate.

スクリーンショット 2020-11-02 23.30.30.png

Access http: // localhost: 3000 / with rails s! If you can confirm that you are standing up firmly

routes.rb


root 'reservations#index'

Let's change the route path to the reservation list page. スクリーンショット 2020-11-02 23.33.42.png

Next, we will arrange the view of simple_calendar.

rails g simple_calendar:views
create  app/views/simple_calendar
      create  app/views/simple_calendar/_calendar.html.erb
      create  app/views/simple_calendar/_month_calendar.html.erb
      create  app/views/simple_calendar/_week_calendar.html.erb

Three files have been created, but this time I would like to use mouth.

application.css


 *= require simple_calendar
# simple_Read calendar css

ruby:reservations.index.html.erb



<p id="notice"><%= notice %></p>

<h1>Reservations</h1>

<table>
  <thead>
    <tr>
      <th>Name</th>
      <th>Start time</th>
      <th colspan="3"></th>
    </tr>
  </thead>

  <tbody>
    <% @reservations.each do |reservation| %>
      <tr>
        <td><%= reservation.name %></td>
        <td><%= reservation.start_time %></td>
        <td><%= link_to 'Show', reservation %></td>
        <td><%= link_to 'Edit', edit_reservation_path(reservation) %></td>
        <td><%= link_to 'Destroy', reservation, method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>
    <% end %>
  </tbody>
</table>

<br>

<!--from here--!>
<%= month_calendar events: @reservations do |date, reservations| %>
  <%= date %>

  <% reservations.each do |reservation| %>
    <div>
      <%= reservation.start_time.hour %>:<%= reservation.start_time.min %>
      <%= reservation.name %>
    </div>
  <% end %>
<% end %>
<!--Add up to here--!>

<%= link_to 'New Reservation', new_reservation_path %>

スクリーンショット 2020-11-02 23.49.48.png I think I was able to display the calendar!

スクリーンショット 2020-11-02 23.52.22.png

If you add a reservation, it will be reflected in the calendar ♪

This alone is boring, so let's add validation.

Make your own special validation

I want to limit the number of reservations this time, so reservations are not possible on Saturdays and Sundays. Past dates cannot be selected I would like to make a tremendous special validation that the reservation time zone is only 13:15 and 19:15!

reservation.rb


class Reservation < ApplicationRecord
  validates :name, presence: true
  validate :date_before_start
  validate :start_time_not_sunday
  validate :start_time_not_saturday
  validate :time_only
  validates :start_time, uniqueness: { message: 'Is reserved by another user' }

  def date_before_start
    errors.add(:start_time, "Cannot select past dates") if start_time < Date.today
  end

  def start_time_not_sunday
    errors.add(:start_time, "Cannot select sunday") if start_time.sunday?
  end

  def start_time_not_saturday
    errors.add(:start_time, "Cannot select saturday") if start_time.saturday?
  end
  
  def time_only
    if hour_only_1 && min_only
      true
    elsif hour_only_2 && min_only
      true
    else
      errors.add(:start_time, "(time)Is 13:15 or 19:Will be 15")
    end
  end

  def hour_only_19
    start_time.hour == 19
  end

  def hour_only_13
    start_time.hour == 13
  end

  def min_only
    start_time.min == 15
  end
end

date_before_time indicates that you cannot select a date in the past. Saturdays and Sundays cannot be reserved with start_time_not_subday and start_time_notsaturday! start_time.sunday? Etc. date_time (type) Sunday? Is it? It can be expressed like this!

From here is the production! !! We will limit reservations to two hours, 13:15 and 19:15.

Since you can specify the time with start_time.hour and the number of minutes with start_time.min We will limit this to 13:00, 19:00 and 15 minutes!

Create a time_only method to limit the time! !! Also, by setting start_time to uniqqueness: true, reservations cannot be made even at the same time, so the maximum number of reservations that can be made per day is 2! !!

Extra edition

This time it was a very special shape, so I made my own validation, but for datetime validation There are useful gems such as validates_timeliness! https://github.com/adzap/validates_timeliness

# in Gemfile
gem 'validates_timeliness', '~> 5.0.0.beta1'

# Run bundler
$ bundle install

For example, if you want to limit the reservation time between 9 and 5 o'clock validates_time :booked_at, between: ['9:00am', '5:00pm'] You can easily implement it by writing it in a form like this! !!

Recommended Posts

Implement a reservation system using Rails and simple calendar! Let's add validation to datetime!
Try to implement tagging function using rails and js
How to implement a circular profile image in Rails using CarrierWave and R Magick
[Rails] I tried to implement "Like function" using rails and js
[Rails] A simple way to implement a self-introduction function in your profile
[Rails] Add a confirmation screen and a completion screen to devise membership registration.
I tried to build a simple application using Dockder + Rails Scaffold
[Rails] Introduce a simple calendar and insert a "confirmation screen" in additional events
How to implement a slideshow using slick in Rails (one by one & multiple by one)
[Rails] How to create a table, add a column, and change the column type
[Rails] [Memo] When to add = to <%%> and when not
[Rails] Let's create a super simple Rails API
How to implement image posting using rails
A memo to simply create a form using only HTML and CSS in Rails 6
[Rails] How to create a graph using lazy_high_charts
Add a tag function to Rails. Use acts-as-taggable-on
How to implement a like feature in Rails
I tried to implement a server using Netty
I tried to decorate the simple calendar a little
Add a subview to your animation layer using lottie-ios
Calendar implementation and conditional branching in Rails Gem simple calendar
How to convert A to a and a to A using AND and OR in Java
[Rails] Create sitemap using sitemap_generator and deploy to GAE
[Rails 6] Add images to seed files (using Active Storage)
Rails learning How to implement search function using ActiveModel
[Rails] How to install a decorator using gem draper
Let's create a file upload system using Azure Computer Vision API and Azure Storage Java SDK
A story I was addicted to before building a Ruby and Rails environment using Ubuntu (20.04.1 LTS)