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

我有scaffold event start_time:datetime title

Events_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


谢谢:)

最佳答案

这可能是一种解决方案。有两个查询...

控制者

 def index
    @upcoming_events = Event.where("start_time > ?", Time.now)
    @past_events = Event.where("start_time < ?", Time.now)
 end


视图

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

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


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

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

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

10-16 16:50