Co2
Co2

Reputation: 353

RoR: How to show a due day instead of due date?

My application contains mobile views with Gmail style divs. Each div has a subject, name and due date.

The Due date column is a normal datetime value.

What I am looking to do is to have the first entries in the view show up as days for the last 6 days and then the rest show as datetime.

For example:

Mon Tues Wed Wed Thurs Fri 2016/11/04 2016/11/03 .....

etc.

How can this be best achieved? I don't think I need to post code for this question but I will if required.

Thanks!

EDIT

Not working...

In view...

<font size="1"><strong>Due:</strong> <%= homework.formatted_date(due) %></font>&emsp;&ensp;

In ApplicationController...(not sure if I have syntax correct...)

helper_method :formatted_date

def formatted_date(due)
  if (Date.today - due).abs < 6
    due.strftime('%A')
  else
    due.strftime('%Y/%m/%d')
  end
end

The view loads but the dates show same as before...

Upvotes: 0

Views: 71

Answers (1)

SteveTurczyn
SteveTurczyn

Reputation: 36860

make a helper for your view... put it in ApplicationController

helper_method :formatted_date

def formatted_date(item_date)
  if (Date.today - item_date).abs < 6
    item_date.strftime('%A')
  else
    item_date.strftime('%Y/%m/%d')
  end
end

Then in your view, instead of showing the object_date field show formatted_date(object_date)

Upvotes: 2

Related Questions