Rails 4 x Bootstrap 3 Datetimepicker: replace defa

2019-09-13 18:39发布

问题:

In my Rails 4 app, which already uses Bootstrap 3, I just added Datetimepicker by eonasdan.

I carefully followed the instructions offered here and there to install the plugin.

Here is my setup:

Gemfile

gem 'momentjs-rails'
gem 'bootstrap3-datetimepicker-rails'

application.js

//= require jquery
//= require jquery.turbolinks
//= require jquery_ujs
//= require bootstrap
//= require bootstrap/dropdown
//= require moment
//= require bootstrap-datetimepicker
//= require turbolinks
//= require_tree .

application.scss

@import "bootstrap-sprockets";
@import "bootstrap";
@import 'bootstrap/theme';
@import 'bootstrap-datetimepicker';
@import "font-awesome-sprockets";
@import "font-awesome";
@import "simple_calendar";
@import "custom.scss";
@import "custom/**/*"

And I do have restarted my server.

Now, I am trying to use the plugin in my Posts#New and Posts#Edit views form, to replace the current datetime_select field:

<div class="field">
  <%= f.label :date, "DATE" %>
  <%= f.datetime_select :date %>
</div>

This is where I am after my first attempt:

<div class="form-group">
  <div class='input-group date' id='datetimepicker'>
    <%= f.label :date, "DATE & TIME" %>
      <%= f.text_field :date, :class => "form-control"%>
      <span class="input-group-addon">
        <span class="glyphicon glyphicon-calendar"></span>
      </span>
  </div>
  <script type="text/javascript">
    $(function () {
      $('#datetimepicker').datetimepicker();
    });
  </script>
</div>

Which gives me the following elements in the view:

Although the design is far from perfect, for now, this is not what I am concerned about.

The real problem is that, when I create a post, the date attribute (which is a datetime data type) is set to nil, instead of the value chosen in the datetimepicker.

I looked for similar situations online, and the closest I have found is this Stack Overflow question.

Unfortunately, it is based on slim forms, and I don't know how to "translate" them into regular forms, to be used in a erb.html file.

Any idea of what is wrong with my code?

—————

UPDATE: I have found a way that is actually saving the date to the database when the post is created:

<div class="form-group" id="post_date_time">
      <%= f.label :date, "DATE & TIME" %>
      <div class='input-group date' id='datetimepicker'>
        <%= f.text_field :date, :class => "form-control"%>
          <span class="input-group-addon">
              <span class="glyphicon glyphicon-calendar"></span>
          </span>
      </div>
    </div>
    <script type="text/javascript">
      $(function () {
        $('#datetimepicker').datetimepicker();
      });
    </script>

However, it does not seem to work properly on the edit form, since I don't get the datetimepicker populated with the date of the post. Am I missing something here?

—————

回答1:

To get the datetime populated on your edit page you can do this in the javascript setup:

<script type="text/javascript">
  $(function () {
    $('#datetimepicker').datetimepicker({
      defaultDate: "<%= @post.date %>",
      format: "YYYY-MM-DD hh:mm a Z"
    });
  });
</script>

I have the working code here: Github - Rails Datetimepicker Example