在 Rails 中,我可以将两个实例变量添加到使用简单日历 gem 的循环中吗

In Rails can i add two instace variables to a loop working with the Simple Calender gem

我正在尝试为我登录和下班创建一个简单的时间 table。我正在使用 simple_calendar gem 我的主页索引中有一个压延机和两个用脚手架创建的控制器。 rails g 支架时钟名称 start_time:date 和 rails g 脚手架时钟名称 start_time:date.

我的 home/index 文件如下所示:

<%= month_calendar events: @clock_ins do |date, clock_ins| %>
<%= date %>
<% clock_ins.each do |clock_in| %>
  <div>
    <button type="button" class="btn btn-success">
    <%= clock_in.name %>
    <%= clock_in.start_time.strftime("%I:%M %P") %>
  </button>
   
  </div>
<% end %>

<% 结束 %>

我似乎无法弄清楚如何让打卡数据显示在日历上以及打卡数据上。是否可以在 Rails 中迭代两个控制器?

根据你现在的情况,你可以将 @clock_ins@clock_outs 组合成一个数组并同时迭代这两个数组。确保在控制器操作中添加 @clock_outs 实例变量

<%= month_calendar events: [*@clock_ins, *@clock_outs] do |date, events| %>
  <%= date %>
  <% events.each do |event| %>
    <div>
      <% if event.is_a?(ClockIn) %>
        <button type="button" class="btn btn-success">
          <%= event.name %>
          <%= event.start_time.strftime("%I:%M %P") %>
        </button>
      <% elsif event.is_a?(ClockOut) %>
        <button type="button" class="btn">
          <%= event.name %>
          <%= event.start_time.strftime("%I:%M %P") %>
        </button>
      <% end %>
    </div>
  <% end %>
<% end %>

您也可以尝试为 clock-ins 和 clock-outs 使用 一个 模型。也许将其命名为 CalendarEvent,其中有一列说明它是 clock_in 还是 clock_out(确保不要将该列命名为 type,这会使 ActiveRecord 崩溃)。