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
Rails 6.0.3 ruby 2.7.0
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.
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.
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 %>
I think I was able to display the calendar!
If you add a reservation, it will be reflected in the calendar ♪
This alone is boring, so let's add 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! !!
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