0

I would like to save the form when the date is selected from datepicker without a submit button.

However, I do not know what shall I type into update.js.erb to assist me to do that.

Here are my codes:

show.html.erb

<%= form_for(@place , :html => { :multipart => true }) do |f| %>
div class="field">
<%= f.label :select_start %><br />
<%= f.text_field :select_start, id: "place_select_start" %>
</div>

javascript

$(function() {
                  var startDate="<%= @place.start_date %>";
                  var endDate="<%= @place.end_date %>";


                  $("#place_select_start").datepicker({

                    value: new Date(),
                    minDate: new Date(startDate),
                    maxDate: new Date(endDate),
                    dateFormat: 'yy-mm-dd',
                    beforeShowDay: unavailable


                  })
                });
Lt_Shade
  • 594
  • 1
  • 7
  • 18
sadocean
  • 5
  • 5
  • Does the javascript in `update.js.erb` gets included when rendering the `show` action? I think `update.js.erb` is rendered when you call the url `PUT place/1/update.json`. – zwippie Nov 11 '13 at 11:33
  • i have not code update.js.erb as I am not sure what needs to be coded inside. – sadocean Nov 11 '13 at 11:46

1 Answers1

0

Something like this will trigger the form submit when the date is changed:

$('#place_select_start').datepicker({
  onSelect: function() {
    $(this).closest('form').submit();
  }
});
zwippie
  • 13,369
  • 3
  • 34
  • 53
  • hi, it works. thanks for your help! :) however, the pages refreshes. is there any way to not make it refresh and update? – sadocean Nov 11 '13 at 11:36
  • You should use `ajaxSubmit()` instead of `submit()` then. For this you need the ['jQuery Form Plugin'](http://jquery.malsup.com/form/). Also have a look [here](http://stackoverflow.com/a/1960245/2637573). – AWM Nov 11 '13 at 12:01
  • Or you could use Turbolinks with RoR4. This will 'hide' the refresh. – AWM Nov 11 '13 at 12:07
  • the ajaxSubmit() does not work. after i use it, the datepicker does not come out. – sadocean Nov 11 '13 at 12:44
  • i remove turbolinks as it caused some of my javascript files to not load properly. – sadocean Nov 11 '13 at 12:45