Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create time steps array in ruby on rails in 30 or 60 minute increments

I am creating a simple appointment keeper in Rails 4, and I need to create an array of times for the user to choose between a min/max. The user can also choose if they are creating a 30 or 60 min appointment.

I tried to create the array like this

 def time_range
     return [] if time_start.blank? || time_end.blank?
    (time_start.to_time .. time_end.to_time).to_a
  end

but I keep getting the error

can't iterate from Time

I don't know how I could break this out into increments, either.

I'm just showing them as a list

  ul.list-unstyled
    - @meeting.time_range.each do |calendar_time|
      li 
        = calendar_time
like image 479
NothingToSeeHere Avatar asked Nov 29 '25 01:11

NothingToSeeHere


1 Answers

Make use of step

(Time.now.to_i..1.day.from_now.to_i).step(30.minutes).each do |time|
    puts Time.at(time)
end

So the method will be

def time_range
  return [] if time_start.blank? || time_end.blank?
  (time_start.to_time.to_i..time_end.to_time.to_i)
end

And you can use it as

ul.list-unstyled
  - @meeting.time_range.step(30.minutes).each do |calendar_time|
    li 
      = Time.at(calendar_time)
like image 148
Deepak Mahakale Avatar answered Dec 01 '25 15:12

Deepak Mahakale