0

I am having some trouble iterating through my JSON array. I want to set a certain Schedule based on what the client sends to the server.

The JSON from the client will be sent in this format:

{
"Schedule": 
  {
    "monday": [
        12,
        15
    ]
  , 
  "tuesday": [
        10,
        16
    ]
  }
}

I would like to iterate like this:

params[:Schedule].each do |day| do
   day.each do |time|
    schedule.add_recurrence_rule(IceCube::Rule.weekly.day(0).hour_of_day(time))
   end
end

However this doesn't work, since when I print params[:Schedule].each it prints out monday, 12, 15, tuesday, 10, 16 etc....

Does anyone have a solution for this?

1 Answer 1

5

params["Schedule"] gets an hash, not an array. So your block will have a key (the day as a name) and an array

params["Schedule"].each do |day_name, day_schedule|
  # to do
end
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! Works perfectly :-)
My pleasure Sir ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.