32

I'm trying to use the jQuery Datepicker to set a date field in my Ruby on Rails form, but I can't work out how to do it. Can someone point me in the right direction?

ben
  • 29,229
  • 42
  • 124
  • 179

7 Answers7

18

I've built a gem to handle this:

https://github.com/albertopq/jquery_datepicker

Hope it helps somebody else.

albertopq
  • 201
  • 2
  • 5
  • 1
    This does not seem to work if you have the asset pipeline enabled, as you cannot install jquery the way the plugin requires unless you disable the pipeline. – stan Nov 28 '11 at 01:27
  • I've updated the gem. It should work fine now with the pipeline. – albertopq Feb 24 '12 at 09:04
  • 2
    https://github.com/joliss/jquery-ui-rails would be the current better choice because you can pick and choose modules to include. Your gem seems to include the entire ui library. Love your timepicker extension! – Damien Roche Nov 09 '12 at 06:38
16

Ryan Bates has a really great explanation of all of this:

http://railscasts.com/episodes/213-calendars (older version you can watch for free) http://railscasts.com/episodes/213-calendars-revised (revised version you need a subscription to watch)

Darryl
  • 5,907
  • 1
  • 25
  • 36
Christian Bankester
  • 2,044
  • 2
  • 15
  • 18
5

As of now, I'd recommend jquery-ui-rails gem above the other answers for the simple reason you can include only the datepicker assets without the entire jquery ui library!

https://github.com/joliss/jquery-ui-rails

Damien Roche
  • 13,189
  • 18
  • 68
  • 96
4

Update for Rails 5.x

Step 1. Install the jquery-ui-rails gem

# gemfile
gem 'jquery-ui-rails'

# app/assets/javascripts/application.js
//= require jquery-ui/widgets/datepicker

# app/assets/stylesheets/application.css
*= require jquery-ui/datepicker

Step 2. Assuming you have a resource called Event with a date or datetime field called :start, then in the form make the :start field a text field.

# app/views/events/_form.html.erb
<%= f.label :start, 'Start Date' %>
<%= f.text_field :start %>

Step 3. Add Javascript (in CoffeeScript syntax here). Line1) If you have Turbolinks enabled (Turbolinks speeds up Rails page loads when you click on a link by loading it with AJAX) you need to add a wrapper or the JavaScript function won't load when you navigate to the page from an internal link.

Line2) You are targeting the element id in your form. Rails automatically assigns an id to the form input by combining the Model name and field name.

Line3) You need to set the dateFormat because jQuery-UI and Rails use different default date formats.

# app/assets/javascripts/event.coffee
$(document).on 'turbolinks:load', ->
  $('#event_start').datepicker
    dateFormat: 'yy-mm-dd'

For Rails 4 everything is the same except the first line in the JavaScript (or CoffeeScript) file. The wrapper to load the JavaScript when Turbolinks is enabled in Rails 4 is:

$(document).on "page:change", ->
Steve Carey
  • 2,876
  • 23
  • 34
  • Can you add an example for Rails 4? I'm in the process of updating an app from 3 to 4 and the `jquery_datepicker` stopped working so I'm going to use your solution. If I'm using the turbolinks gem in rails 4, this solution should work for me still right? – aarona Apr 14 '17 at 13:27
  • 1
    DJTripleThreat - For Rails 4 everything would be the same except the JavaScript (or in this case CoffeeScript) wrapper to execute with Turbolinks. I've edited my answer above to add the change. – Steve Carey Apr 14 '17 at 19:46
  • I am dealing with this exact issue right now and set up a simple dummy app with a scaffold - running rails 5.0.3 - and i get an ActionController Exceiption that it can't find the datpicker widget... – MageeWorld May 28 '17 at 00:41
  • did find information [here](https://github.com/jquery-ui-rails/jquery-ui-rails/blob/v5.0.5/README.md) that did solve the issue - it's possible you can't add specific widgets anymore - example works if you just require the whole ui – MageeWorld May 28 '17 at 00:49
4

I used albertopq's jquery_datepicker that albertopq mentions and it works with regular forms, nested attributes, etc. It made things very easy for me. jquery_datepicker also correctly passes options to datepicker's necessary javascript calls.

Here's an example from one of my nested forms:

<%= f.datepicker 'availability', :minDate => 0, :maxDate => "+8Y", :tab_index => autotab %>

minDate and maxDate are passed to datepicker and tab_index is put into the text field html. (autotab is just my form helper to advance the tab + 1...better for me than hardcoding it).

1

It may be a bit late but I use a simple gem :

Gemfile: gem 'jquery-ui-rails'

Application.js: //= require jquery-ui

Application.css *= require jquery-ui

And then: Bundle install

Source : https://github.com/joliss/jquery-ui-rails

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Francois
  • 1,367
  • 1
  • 15
  • 23
1

If using Rails 3.0+, you shouldn't need to do anything other than include jQuery and jQuery UI because jQuery is the default JavaScript framework.

If using Rails earlier than 3.0 or using Prototype (or something else), you'll need to use jQuery in noConflict mode. Make sure you include jQuery after Prototype (your other framework) has been loaded using something similar to:

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="jqueryui.js"></script>
<script type="text/javascript">
  jQuery.noConflict();
  jQuery(document).ready(function($) {
    // here the $ function is jQuery's because it's an argument
    // to the ready handler
    $('#dateField').datepicker();
  });
  // here the $ function is Prototype's
</script>
tvanfosson
  • 524,688
  • 99
  • 697
  • 795