Search code examples
javascriptjqueryruby-on-railshashjquery-ui-timepicker

Putting a dynamic hash in JQuery using rails


I am newish to rails and new to JQuery. I am using jquery.timepicker with rails and it takes hashes to block out certain time ranges in a given list of times (ie, between 8am and 6pm, 1pm-2pm is not available). On a static page, the JQuery is written as so:

  <script>
    $('#someTextfieldPair .time').timepicker({
        'timeFormat': 'g:ia',
        'disableTimeRanges': [
          ['11:00am', '12:00pm'],
          ['3:00pm', '4:00pm']
        ]
     });
   </script>  

Instead of static times, I'm trying to create the hashes based on the values @other_events.time_start and @other_events.time_end. I've tried this, but I don't really know how to combine JQuery and rails.

 <script>
    $('#someTextfieldPair .time').timepicker({
        'timeFormat': 'g:ia',
        'disableTimeRanges': [
        <%= @other_events.each do |e| %>
          ['<%= e.time_start.strftime("%l:%M %P") %>', '<%= e.time_end.strftime("%l:%M %P") %>'],
          <%end %>
        ]
     });
   </script>  

I am using this jquery.timepicker: http://jonthornton.github.io/jquery-timepicker/


Solution

  • I suggest you to check https://github.com/gazay/gon gem, it's fantastic for rails: it generates a javascript object on top of page, so you can access your time ranges (per page!) in javascript like you would do with any variable.