gpt4 book ai didi

ruby-on-rails - 区分过去的事件和 future 的事件

转载 作者:行者123 更新时间:2023-12-04 06:27:08 26 4
gpt4 key购买 nike

问题:如何显示具有 start_time<time.now 的事件& start_time>time.now在两个不同的表中?

我有scaffold event start_time:datetime title

事件 Controller :

def index
@event = Event.all
end

index.html.haml:

 %table
%thead
%tr
%th Start_time
%th Title

%tbody
- @events.each do |event|
%tr
%td= event.start_time
%td= event.title

谢谢:)

最佳答案

您可以简单地为过去和 future 的事件定义范围:

class Event < ActiveRecord::Base
scope :in_the_past, { where('start_time < ?', Time.now }
scope :in_the_future, { where('start_time > ?', Time.now }
end

那么你的观点将是:

_events_table.html.haml

%table
%thead
%tr
%th Start_time
%th Title

%tbody
- events.find_each do |event|
%tr
%td= event.start_time
%td= event.title

index.html.haml

/ Past events
= render 'events_table', events: Event.in_the_past
/ Future events
= render 'events_table', events: Event.in_the_future

关于ruby-on-rails - 区分过去的事件和 future 的事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35991031/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com