Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6861747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:34:28+00:00 2026-05-27T02:34:28+00:00

I have the following view code which displays events, along with headers informing the

  • 0

I have the following view code which displays events, along with headers informing the user if the following events occured today, yesterday, or in the past week.

It all works fine, but I would prefer to refactor it out of the view, I can’t put it in a helper because it needs to maintain the variables state between loop iterations. Any idea how to tidy this up? thanks

<% displayed_week_already = false %>
<% displayed_yesterday_already = false %>
<% displayed_today_already = false %>

<% @events.each do |event| %>

  <%= event.title &>

  <% if (Time.now - 1.week) > event.created_at && !displayed_week_already %>
    <% displayed_week_already = true %>
    Events in past week
  <% elsif (Time.now - 1.day) > event.created_at && (Time.now - 2.day) < event.created_at && !displayed_yesterday_already %>
    <% displayed_yesterday_already = true %>
    Events yesterday
  <% elsif (Time.now - 1.day) < event.created_at && !displayed_today_already %>
    <% displayed_today_already = true %>
    Events in past day
  <% end %>

<% end %>

Expected output would be:

Events in past day
John created a new artice
Dan commented on article 1
Sue deleted article 2

Events yesterday
Sue created a new article

Events in past week
Carol was promoted
Dan cerated a new article

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T02:34:29+00:00Added an answer on May 27, 2026 at 2:34 am

    I might do something like the following. I am leaving out the implemented methods defined on the model itself.

    existing view

    <%= render "events", :title => "Events in past day",  :events => from_today(@events) %>
    <%= render "events", :title => "Events yesterday",    :events => from_yesterday(@events) %>
    <%= render "events", :title => "Events in past week", :events => from_this_week(@events) %>
    

    _events.html.erb

    <% if events.present? %>
      <%= title %>
      <% events.each do |event| %>
        <%= event.title %>
      <% end %>
    <% end %>
    

    app/helpers/event_display_helper.rb

    module EventDisplayHelper
      def from_today(events)
        events.select{|x| x.happened_today?}
      end
    
      def from_yesterday(events)
        events.select{|x| x.happened_yesterday?}
      end
    
      def from_this_week(events)
        events.select{|x| x.happened_this_week_before_yesterday?}
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have the following code which will displays result in a UItable view along
I Have following code: Controller: public ActionResult Step1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public
I have the following code in a strongly-typed View in my application: <td> <label
I have the following code in my index view. latest_entry_list = Entry.objects.filter(is_published=True).order_by('-date_published')[:10] for entry
I have the following code in a view to get some of the information
I have the following code in a partial view (using Spark): <span id=selectCount>0</span> video(s)
i have the following code in an asp.net mvc view. <% = Html.DropDownList(Filter, new
I have created an alert view with two buttons using the following code: UIAlertView
I have the following code which should make drawFrame be called every frame but
I have one grid view in my web application. I use the following code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.