LoginSignup
7
16

More than 3 years have passed since last update.

[Rails]Simple Calendarでカレンダー表示

Last updated at Posted at 2018-07-25

環境

ruby 2.4.4
rails 5.1.6

Gem

Simple calendar
説明を参考にしつつ

Gemfile
gem "simple_calendar", "~> 2.0"
bundle install

表示

コントローラー

tasks_controller.rb
@task = Task.all

この辺は適宜変更

ビュー

今回は週ごとの表示にします。
月ごと表示はweek_calendarmonth_calendarにするだけ。

show.html.erb
  <%= week_calendar @task do |date, tasks| %>
    <%= date %>
    <% tasks.each do |task| %>
      <div>
        <%= task.title %>
      </div>
    <% end %>
  <% end %>

Taskテーブルにstart_timeがあればこれでOKだが、ほぼ無いと思うので以下

hoge.html.erb
  <%= week_calendar(attribute: :input_date, events: @task, number_of_weeks: 2) do |date, tasks| %>
    <%= date %>
    <% tasks.each do |task| %>
      <div>
        <%= task.title %>
      </div>
    <% end %>
  <% end %>

attribute: :column カラムの指定
events: @hoge インスタンス変数指定
number_of_weeks: int int週分を表示

あとは<div>の中身をイイッ感じにしてあげればOK

参考

【Rails】Simple Calendarを使ったカレンダー表示
https://qiita.com/mikaku/items/0971442a7308dc669122

他に使えそうなカレンダーGem

carender →使い方がよくわからなかった・・・
fullcalendar →JS、JQuery満載でわからない

最後に

Googleアカウントが使えるなら、Googleカレンダー使った方が楽です!
カレンダーの埋め込みも出来ます。

7
16
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
16