0

I am trying to

I have a starts_at:datetime field and I want to fill the form with using bootstrap-datetimepicker

Here is my form:

= simple_form_for @event do |f|
    = f.input :title
    = f.input :starts_at
    = f.button :submit

My main.js file:

 $(document).ready(function(){
        $(".form_datetime").datetimepicker({format: 'yyyy-mm-dd'});
 });

How can I give the starts_at the datettimepicker input style?

2 Answers2

1

You'll have to call the .datetimepicker() method on the input element that you want to give the datepicker. Simple_form should generate an element with the id "event_starts_at", so try:

$("#event_starts_at").datetimepicker({format: 'yyyy-mm-dd'});

If that doesn't work or if you just want to give the input element a different id, you can override it by passing the option:

= f.input :starts_at, id: "event_starts_at"

mlovic
  • 864
  • 6
  • 11
0

It's generally a bad idea to throw too much JavaScript into your view. Consider moving JS into a seperate file like this:

%script{:type => "text/javascript", :src => "/js/myscript.js"}

Without seeing the rest of your code, my best guess is that the JavaScripts are not being loaded in the correct order. JQuery and then Bootstrap would have to be loaded before this DateTime Picker would work.

You can read this SO article for information on script ordering.

Also, consider putting the DOM Handling code in a document.ready callback, otherwise, the code may run before it has a DOM element to attach to.

$(document).ready(function(){
    your_code_goes_here();
});
Community
  • 1
  • 1
Trajanson
  • 441
  • 3
  • 11