Enrollment Page

Hyeonseop-Noh·2022년 3월 3일
0

Install simple-calendar

add the code below to Gemfile

gem "simple_calendar", "~> 2.4"

start bundle

$ bundle install

Set scoffild

$ gem g scoffild Schedules tutor_id:integer start_time:datetime active:integer

parameters - tutor_id, start_time, active(1: available, 2: unavailable)

home.html.erb

Set the home

<h1>Enrollment Page</h1>
<p id="notice" style="color: red"><%= notice %></p>
<hr>
<h3>Input your class format</h3>
<form action="/calendar" method="get">
  Find your tutor (id): <input type="string" name="tutor_id" placeholder="1 ~ 5"/><br>
  Set the time (min): <input type="string" name="time" placeholder="20 or 40"/><br>
  <input type="submit"/>
</form>
<br>
<h3>For tutors</h3>
<form action="/tutor_schedules" method="get">
  Type your tutor id: <input type="string" name="tutor_id" placeholder="1 ~ 5"/><br>
  <input type="submit"/>
</form>
<br>
<a href="/index">Show total schedules</a>

index.html.erb

Load total week-calendar

<h1>Schedules</h1>

<%= link_to 'New Schedules', new_schedule_path %>
<a href='/home'> Back to home </a>
<br><br>

<%= week_calendar events: @schedules do |date, schedules| %>
  <div style="font-weight: 700">
  <%= date %>
  </div>
  <hr>
  <% schedules.each do |schedules| %>
    <div>
      <%= schedules.tutor_id %> / 
      <%= schedules.start_time.hour %>:<% if schedules.start_time.min == 0 %>00
      <% else %><%= schedules.start_time.min %>
      <% end %> / 
      <span style="font-weight: 600">
      <% if schedules.active == 1 %>
      Available
      <% else %>
      Unavailable
      <% end %>
    </div>
  <% end %>
<% end %>

tutor_schedules.html.erb

Tutor-schedules

<h1>Schedules</h1>
<%= link_to 'New Schedules', new_schedule_path %>
<a href='/home'> Back to home </a>
<br><br>

<%= week_calendar events: @schedules do |date, schedules| %>
  <div style="font-weight: 700">
  Date
  <%= date %>
  </div>
  <hr>
  <% schedules.each do |schedules| %>
    <div>
      <%= schedules.tutor_id %>
      <%= schedules.start_time.hour %>:<% if schedules.start_time.min == 0 %>00
      <% else %><%= schedules.start_time.min %>
      <% end %>
      <% if schedules.active == 1 %>
      Available
      <% else %>
      Unavailable
      <% end %>
      <%= link_to 'Show', schedules %>
    </div>
  <% end %>
<% end %>

calendar.html.erb

Enroll by tutor_id, class running time

<h1>Schedules</h1>

<a href='/home'> Back to home </a><br>
<br>

<%= week_calendar events: @schedules do |date, schedules| %>
  <div style="font-weight: 700">
  Date
  <%= date %>
  </div>
  <hr>

  <% if @time == "20" %>
    <% schedules.each do |schedules| %>
      <div>
        <%= schedules.tutor_id %>
        <%= schedules.start_time.hour %>:<% if schedules.start_time.min == 0 %>00
        <% else %><%= schedules.start_time.min %>
        <% end %>
        <% if schedules.active == 1 %>
        <span style="color: green; font-weight: 600">Available</span><br>
        <% else %>
        <span style="color: gray; font-weight: 600">Unavailable</span><br>
        <% end %>
      </div>
    <% end %>
  
  <% elsif @time == "40" %>
    <% schedules.each do |schedules| %>
      <div>
        <%= schedules.tutor_id %>
        <%= schedules.start_time.hour %>:<% if schedules.start_time.min == 0 %>00
        <% else %><%= schedules.start_time.min %>
        <% end %>
        <% if schedules.active == 1 %>
          <%= render :template => "schedules/search_next", :locals => {:@next_time => schedules.start_time + 30*60, :@sheet => @schedules} %>
        <% elsif schedules.active == 2 %>
          <span style="color: gray; font-weight: 600">Unavailable</span><br>
        <% end %>
      </div>
    <% end %>
  <% end %>

<% end %>

For 40 min class, pass the parameter of 'schedules.start_time + 30*60(sec)' to search_next.html.erb

search_next.html.erb

Search if next class is available

<% @sheet.each_with_index do |schedules, index| %>
  <% if schedules.start_time == @next_time %>
    <% if schedules.active == 1 %>
      <span style="color: green; font-weight: 600">Available</span><br>
    <% elsif schedules.active == 2%>
      <span style="color: gray; font-weight: 600">Unavailable</span><br>
    <% end %>
  <% end %>
<% end %>

schedules_controller.rb

class SchedulesController < ApplicationController
  before_action :set_schedule, only: %i[ show edit update destroy ]

  # GET /schedules or /schedules.json
  def home
  end

  def calendar
    @id = params[:tutor_id]
    @time = params[:time]
    @schedules = Schedule.where(tutor_id: @id)
    if (@id.to_i<1) || (@id.to_i>5)
      redirect_to '/home', notice: "Tutor not found."
    elsif (@time.to_i != 20) && (@time.to_i != 40)
      redirect_to '/home', notice: "Class duration not found."
    end
  end

  def tutor_schedules
    @id = params[:tutor_id]
    @schedules = Schedule.where(tutor_id: @id)
  end

  def index
    @schedules = Schedule.all
  end

  # GET /schedules/1 or /schedules/1.json
  def show
  end

  # GET /schedules/new
  def new
    @schedule = Schedule.new
  end

  # GET /schedules/1/edit
  def edit
  end

  # POST /schedules or /schedules.json
  def create
    @schedule = Schedule.new(schedule_params)

    respond_to do |format|
      if @schedule.save
        format.html { redirect_to schedule_url(@schedule), notice: "Schedule was successfully created." }
        format.json { render :show, status: :created, location: @schedule }
      else
        format.html { render :new, status: :unprocessable_entity }
        format.json { render json: @schedule.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /schedules/1 or /schedules/1.json
  def update
    respond_to do |format|
      if @schedule.update(schedule_params)
        format.html { redirect_to schedule_url(@schedule), notice: "Schedule was successfully updated." }
        format.json { render :show, status: :ok, location: @schedule }
      else
        format.html { render :edit, status: :unprocessable_entity }
        format.json { render json: @schedule.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /schedules/1 or /schedules/1.json
  def destroy
    @schedule.destroy

    respond_to do |format|
      format.html { redirect_to schedules_url, notice: "Schedule was successfully destroyed." }
      format.json { head :no_content }
    end
  end

  # DELETE all schedules
  def total_destroy
    @schedules = Schedule.all
    @schedules.each do |schedule|
      schedule.destroy
    end
    redirect_to '/index'
  end

  def import
    Schedule.import(params[:file])
    redirect_to schedules_path, notice: "Schedules Added Successfully"
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_schedule
      @schedule = Schedule.find(params[:id])
    end

    # Only allow a list of trusted parameters through.
    def schedule_params
      params.require(:schedule).permit(:tutor_id, :start_time, :active)
    end
end

Results

home page

enroll class page

total classes

References

Ruby on Rails guide

https://rubykr.github.io/rails_guides/getting_started.html

Install Ruby on Rails

https://ringle.notion.site/rails-mac-a9f4437721a74b19baf9c526b1823b9a

Implement simple-calendar

https://linuxtut.com/implement-a-reservation-system-using-rails-and-simple-calendar!-let%27s-add-validation-to-datetime!-2a900/

Simple-calendar github origin guide

https://github.com/excid3/simple_calendar

Render with locals

https://apidock.com/rails/ActionController/Base/render
https://stackoverflow.com/questions/19907178/rails-partial-locals-not-working

Pass parameters in the 'a href' tag

https://stackoverflow.com/questions/18276073/passing-params-to-href-in-rails

Import CSV file

https://www.youtube.com/watch?v=_NSBm_Q431Y

Ruby on Rails basic

https://www.youtube.com/watch?v=iNrT0O2_MQM&list=PLEBQPmkNcLCIE9ERi4k_nUkGgJoBizx6s

profile
PlanBy Developer

0개의 댓글