Sylar Sylar - 1 year ago 78
Ruby Question

Increase days by 7 in dropdown menu

Title may not be precise but how do you increase a date in a loop for a dropdown menu?

Controller (simple):

f = Foo.find(1)
f.start_date =
f.end_date = 30.days.from_now

@arr = *(f.start_date..f.end_date)


<select class="ui dropdown">
<option value="">Select work week</option>
<% @arr.each do |a| %>
<option value="<%= a %>"><%= a %> to <%= (a + 7.days) %></option>
<% end %>

Expected from dropdown:

(today) to (7th day)
(8th day) to (15th day)
(last day)

How to go about this?

Answer Source

When you create the date range, select only the days with the same weekday value as the first day. That gives you the first date, the eighth date, the 15th date...

@arr = (f.start_date..f.end_date){|day| day.wday == f.start_date.wkday}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download