@ryoohta0601 (大田 凌)

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

simple calenderでイベント表示したい

前提・実現したいこと

rails6でシンプルカレンダーでシフトを登録すると、カレンダーに表示されるようにしたい。
データベースは、postgreSQLで情報は入っています。
カレンダーにだけ表示されないです。

該当のソースコード

index.html.erb
<div class="main">
  <%= render partial: 'shared/header' %>
  <%= link_to new_shift_path do %>
    <button type="button" class="btn btn-primary">シフト提出</button>
  <% end %>
  <button type="button" class="btn btn btn-warning">シフト編集</button>

  <% @shifts.each do |shift| %>
        <%= shift.start_time.strftime("%Y-%m-%d %H:%M") %>
        <%= link_to 'Show', shift %>
        <%= link_to 'Edit', edit_shift_path(shift.id) %>
        <%= link_to 'Destroy',shift_path(shift.id), method: :delete, data: { confirm: 'Are you sure?' } %>
    <% end %>

  <%= month_calendar events: @shifts do |date, shifts| %>
    <%= date.day %>

    <% shifts.each do |shift| %>
      <div>
        <%= link_to shift.start_time, shift %>
      </div>
    <% end %>
  <% end %>
</div>

class ShiftsController < ApplicationController

  def index
    @users = User.all
    @shifts = Shift.all
  end

  def show
    @users = User.all
    @shift = Shift.find(params[:id])
  end

  def new
    @shift = Shift.new
  end

  def create
    Shift.create(shift_params)
    redirect_to shifts_path
  end

  private

  def shift_params
    params.require(:shift).permit(:start_time, :end_time).merge(user_id: current_user.id)
  end

end

試したこと

ほとんど同じ記述でイベントカレンダーを作成してみると、表示されました。

該当のソースコード

index.html.erb
<p id="notice"><%= notice %></p>

<h1>イベント</h1>

<table>
  <thead>
    <tr>
      <th>タイトル</th>
      <th>時間</th>
      <th colspan="3"></th>
    </tr>
  </thead>

  <tbody>
    <% @events.each do |event| %>
      <tr>
        <td><%= event.title %></td>
        <td><%= event.start_time.strftime("%Y-%m-%d %H:%M") %></td>
        <td><%= link_to 'Show', event %></td>
        <td><%= link_to 'Edit', edit_event_path(event.id) %></td>
        <td><%= link_to 'Destroy',event_path(event.id), method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>
    <% end %>
  </tbody>
</table>

<br>

<%= link_to 'イベントを入力', new_event_path %>

<%= month_calendar events: @events do |date, events| %>
  <%= date.day %>

  <% events.each do |event| %>
    <div>
      <%= link_to event.title, event %>
    </div>
  <% end %>
<% end %>
class EventsController < ApplicationController

  def index
    @events = Event.all
  end

  def new
    @event = Event.new
  end

  def show
    @event = Event.find(params[:id])
  end

  def create
    Event.create(event_params)
    redirect_to root_path
  end

  def edit
    @event = Event.find(params[:id])
  end

  def update
    @event = Event.find(params[:id])
    if @event.update(event_params)
      redirect_to root_path, notice: "編集しました"
    else
      render 'edit'
    end
  end

  def destroy
    @event = Event.find(params[:id])
    @event.destroy
    redirect_to root_path, notice:"削除しました"
  end

  private

  def event_params
    params.require(:event).permit(:title, :content, :start_time)
  end
end
0 likes

1Answer

要約すると 以下は表示されず、

NG
<%= month_calendar events: @shifts do |date, shifts| %>
  <% shifts.each do |shift| %>
      <%= link_to shift.start_time, shift %>
  <% end %>
<% end %>

以下は表示された

OK
<%= month_calendar events: @events do |date, events| %>
  <% events.each do |event| %>
      <%= link_to event.title, event %>
  <% end %>
<% end %>

ということでしょうか。

shiftはmethodの予約語だったりしませんか?

2Like

Comments

  1. @ryoohta0601

    Questioner

    ご回答ありがとうございます。
    説明不足でした。
    おしゃる通りで、shiftの方は、表示されず、eventでは表示されます。

    カレンダー外ですと、shiftのほうも表示されます。
  2. @ryoohta0601

    Questioner

    自己解決致しました。
    end_timeが予約語?だったらしく、out_timeにするとできました。
    ありがとうございました!
  3. よく見つけられましたね!おめでとうございます。

Your answer might help someone💌