Sakshi Jain
Sakshi Jain

Reputation: 420

setting minDate in datepicker in Rails

I need to apply minDate attribute to the datepicker in Rails App as today's date.

In _admin_controls.html.erb

<%= f.input :end_date, wrapper: :append do %>
  <div id="datepicker" class="datepicker input-group edit-left">
    <%= f.text_field :end_date, :class => "datetime form-control" %>

application.js contains

jQuery(document).on('focus', 'input.datetime', function() {
  opts = {format: 'M dd, yyyy', autoclose: true};
  jQuery(this).datepicker(opts);
  jQuery(".datepicker").css("z-index",10000);
});

What should be the javascript to do so only for _admin_controls.html.erb?

Upvotes: 0

Views: 1959

Answers (4)

akiespenc
akiespenc

Reputation: 305

You can set the rails input field to the following:

 <%= f.datetime_local_field(:end_date, min: Time.now().to_s.slice(0,10) ) %>

Upvotes: 0

Sakshi Jain
Sakshi Jain

Reputation: 420

I have been able to solve the problem. In application.js, I removed the code related to datepicker and its librariies and created a new file datepicker.js

datepicker.js contains

/* bootstrap datepicker */
//= require bootstrap-datepicker/core
//= require bootstrap-datepicker/locales/bootstrap-datepicker.es

jQuery(document).on('focus', 'input.datetime', function() {
  opts =  {format: 'M dd, yyyy', autoclose: true};
  jQuery(this).datepicker(opts);
});

In application.js

//= require datepicker

In _admin_controls.html.erb,

<%= f.input :end_date, wrapper: :append do %>
  <div id="datepicker" class="datepicker input-group edit-left">
    <%= f.text_field :end_date, :class => "datetime form-control", :'data-date-start-date' => Date.current.strftime('%m/%d/%Y') %>

And this disables backdate selection for particularly _admin_controls datepicker. :)

Upvotes: 1

user3620005
user3620005

Reputation:

This should do it

$('#start_date').datepicker({
        minDate: 0,
        onSelect: function (selected) {
            $("#end_date").datepicker("option", "minDate", selected)
        }
    });

Where end date being the one in the other box which allows to select the max one .

Upvotes: 0

Arihant Godha
Arihant Godha

Reputation: 2469

If You are using Jquery then you can set the date like this :

// If you want to set a date then assign it to a variable or create a function and call that function directly.

var currentDate = new Date();

$("#mydate").datepicker().datepicker("setDate", new Date());

Upvotes: 1

Related Questions