ruby-on-rails - 使用 simple_calendar,Rails 在同一日历上的不同事件模型
问题描述
我有两种不同的模型:Note
和Todo
. 每个都有自己不同的属性,我想在日历上显示。
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 %>
解决方案
遇到同样的问题。
在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 %>
怎么样?;)
推荐阅读
- python - 使用python从JSON文件中解析数据并检查它
- mysql - 为什么值不存在时不添加新行?
- java - android 的 SQLite 分组项目和可扩展列表视图
- webpack - Workbox 缓存排除的 html
- git - 如何使用 gitkraken UI 推送特定提交
- java - 如何在 Java 中使用 for(int num : array) 更改原始类型 int 数组的值?
- c# - 在 MVVM WPF 应用程序中显示“vshost32.exe 已停止工作”的对话框
- javascript - 窥探 Angular5 组件无法正常工作
- https - 如何使 web2py 站点成为受信任的站点
- android - AsyncTask 在 SeekBarChangeListener 中不起作用