DEV Community

Samuel Lubliner
Samuel Lubliner

Posted on

Belay Board Simple Part 2: Calendar

https://github.com/Samuel-Lubliner/Belay-Board-Simple

https://github.com/users/Samuel-Lubliner/projects/2

Add Calendar

Add into Gemfile followed by a bundle install:
gem "simple_calendar", "~> 2.4"

Calendar for availability index

<h1>Climb Times</h1>

<%= month_calendar(header: {class: 'calendar-heading'}) do |date| %>
  <div class="card mb-3">
    <div class="card-header">
      <%= date.strftime("%d") %>
      <%= link_to raw('<i class="fas fa-calendar-plus"></i>'), new_availability_path(start_date: date.to_date) %>

    </div>
    <div class="card-body">
      <% day_availabilities = @availabilities.select { |a| a.start_time.to_date == date }.sort_by(&:start_time) %>
      <% day_availabilities.each do |availability| %>
        <p class="mb-1">
          <%= link_to availability.event_name, availability_path(availability), class: "text-dark" %>
          <small class="text-muted"><%= availability.start_time.strftime("%I:%M %p") %></small>
        </p>
      <% end %>
    </div>
  </div>
<% end %>
Enter fullscreen mode Exit fullscreen mode

When creating new availability from the calendar, the start date of the availability should be set to the date corresponding to the date on the calendar.

controllers/availabilities_controller.rb

  def new
    @availability = Availability.new
    @availability.start_time = params[:start_date] if params[:start_date].present?
  end
Enter fullscreen mode Exit fullscreen mode

views/availabilities/_form.html.erb

<div>
    <%= form.label :start_time, style: "display: block" %>
    <%= form.datetime_field :start_time, value: availability.start_time&.strftime("%Y-%m-%dT%H:%M") %>
  </div>

  <div>
    <%= form.label :end_time, style: "display: block" %>
    <%= form.datetime_field :end_time, value: availability.start_time&.strftime("%Y-%m-%dT%H:%M") %>
  </div>
Enter fullscreen mode Exit fullscreen mode

Top comments (0)