Sylar Sylar - 29 days ago 13
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 = Date.today
f.end_date = 30.days.from_now
f.save

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


View:

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


Expected from dropdown:

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


How to go about this?

Answer

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).to_a.select{|day| day.wday == f.start_date.wkday}