首页 > 解决方案 > 使用 simple_calendar,Rails 在同一日历上的不同事件模型

问题描述

我有两种不同的模型:NoteTodo. 每个都有自己不同的属性,我想在日历上显示。

events我试图在我的 上显示不止一种simple_calendar,但不断出现错误,undefined method 'start_time' for #<Note::ActiveRecord_Relation:0x007ffce4182638>

这是我的代码:

<%= calendar number_of_days: 1, events: [@notes, @todos] do |date, notes, todos| %>
  <%= date %>
  <% notes.each do |note| %>
    <div>
      <%= note.title %>
    </div>
  <% end %>
  <% todos.each do |todo| %>
    <div>
      <%= todo.title %>
    </div>
  <% end %>
<% end %>

标签: ruby-on-railsrubyruby-on-rails-4ruby-on-rails-5

解决方案


遇到同样的问题。

events表单和其他一些实用程序的情况下,您使用的关系和语法是正确的,但它可能没有引起这个 gem 开发人员的注意。

解决方案(解决方法)

<%= calendar number_of_days: 1, events: @notes+@todos do |date, events| %>
  <%= date %>
  <% events.each do |event| %>
    <div>
      <%= event.title %>    <%# if both models have same column that you wanna show like this case, otherwise see solution below.%>
    </div>
  <% end %>
<% end %>

或者

<%= calendar number_of_days: 1, events: @notes+@todos do |date, events| %>
  <%= date %>
  <% events.each do |event| %>
    <div>
      <% if event.is_a? Note %>
        <% "show_something_for_note" %>
      <% elsif event.is_a? Todo %>
        <% "show_something_for_todo" %>
      <% end %>
    </div>
  <% end %>
<% end %>

怎么样?;)


推荐阅读