Rails 4 Bootstrap DatePicker class today not working - ruby-on-rails

I have a Rails4 app using gem 'bootstrap-sass'. I'm using the bootstrap datepicker.
This is the Javascript:
$(".datepicker").datepicker
autoclose: true
format: "yyyy-mm-dd"
orientation: "left top"
This is the view code:
<%= f.input :shoot_date, :as => :string, :label => 'Shoot Date:', :input_html => {:class => 'datepicker'}, :wrapper_html => {:style => 'width: 250px'} %>
But, today's date is not highlighted. In the html, it should have:
<td class="day today">23</td>
But it is:
<td class="day">23</td>
The today is missing.
What am I doing wrong? Thanks
PS - I don't want to default to today's date - just have it highlighted on the calendar

Assuming you are using this bootstrap-datepicker, https://github.com/eternicode/bootstrap-datepicker
You should be doing something like this:
$('.my-datepicker-class').datepicker({ todayHighlight: true});
todayHighlight is the field you have to set to true
hope it helps!

Related

how to display current value in select field with using rails gem best_in_place?

I am using rails gem best_in_place.
In select field it doesn't display the current value, instead it displays something like -.
Also, the date field doesn't change date if I select other data.
<% #user.each do |record| %>
<tr>
<td>
<%= best_in_place record, :name, :as => :input %>
</td>
<td>
<%= best_in_place record, :gender, :as => :select, collection: (MUser.pluck(:id, :gender)) %>
</td>
<%= best_in_place record, :dob, :as => :date, class: 'datepicker1 %>
</td>
</tr>
<% end %>
for datepicker used gem datepicker.
Datepicker configuration:
$('.datepicker1').datepicker({
regional: "en",
todayHighlight: true,
changeMonth: true,
todayHighlight: true,
language: "en",
orientation: "auto",
toggleActive: true,
autoclose: true
});
first time Onload page date field:
enter image description here
this image is default view when 1st time page load.
when i click on that field then opened datpicker and selected, but date is selected like 05/16/1923.
and as i set in datepicker js, not working like configured way.
enter image description here
You need to pass the collection of gender like [['Male', 'Male'], ['Female', 'Female']] instead of the collection of id and gender like [[1, 'Male'], [2, 'Female']]. It will display the current value if the collection is right.
Check with this code
<%= best_in_place record, :gender, :as => :select, collection: [['Male', 'Male'], ['Female', 'Female']] %>
Don't know why you want to collect gender from MUser. Still want to use MUser then try this.
<%= best_in_place record, :gender, :as => :select, collection: MUser.pluck(:gender).uniq.map { |g| Array.new(2, g) } %>
Hope it works for you.
The text value is not displayed because the helper does not find it in your collection.
If your attribute is a string (as supposed by #nitin-srivastava) you have to pass a string collection.
And, according to this post Rails 4 Best in place select collection
You have to pass a Hash collection this form :
{"txt1" => "txt1", "txt2" => "txt2", "txt3" => "txt3"}
This works for me.
EDIT :
So you can write this :
<%= best_in_place record, :gender, as: :select, collection: {'Male' => 'Male', 'Female' => 'Female', 'Other' => 'Other'} %>
If record.gender is equal to 'Male', 'Female', or 'Other' it will be displayed.

Rails Make Form Text Field Show Value From Model or Default Value

Rails 5.0.4 ruby 2.5.1p57
I have a form partial that has a date field. It uses the datepicker class.
Currently, I have it set to display the current-date:
<%= form_for(#weight) do |f| %>
<%= f.text_field :workout_date, { :size => 20, :type => 'date', :class => "date-picker", :readonly => false, :value => Date.today } %>
Because I use this form/partial on both the new and edit views, I would like it to show either the date in the model (for an edit) or the current date (for a new record).
I tried this:
<%= f.text_field :workout_date, { :size => 20, :type => 'date', :class => "date-picker", :readonly => false, :value => #weight.workout_date.present? ? #weight.workout_date : Date.today } %>
But, this displays 09/01/2019, when viewing the new form today 09/12/2019.
Ideas on what I'm doing wrong? Can this be done?
Thanks for any tips.
I think I figured this out. In my migration to create the weights model:
t.date "workout_date", :default => Date.today I created this on 09/01/2019, so the form is using this date.
<%= form_for(#weight) do |f| %> I'm passing a weight object for a new record. It must see that default date and use it.
Fix: create a migration to removed that default date. OR, since the new model record does not have an ID, I can do this cheap-hack, which works, until I fix the model:
<%= f.text_field :workout_date, { :size => 20, :type => 'date', :class => "date-picker", :readonly => false, :value => #weight.id.present? ? #weight.workout_date : Date.today } %>
I also do not need the .present?. It works without it.

Rails simple-form datepicker how to add data-behaviour

I have a Rails app using jquery-ui datePicker.
Inside of a simple_form I have two fields. The first one is just a test field. The first one has datePicker working. I'm trying to get "data-behaviour" into the simple_form f.input line. But, I've got the wrong syntax.
<input type="text" data-behaviour='datepicker' >
<%= f.input :exp_date, :data-behaviour => 'datepicker', :as => :string, :label => 'Date', :input_html => {:value => Date.today.to_s} %>
This is my jquery:
$(document).on "focus", "[data-behaviour~='datepicker']", (e) ->
$(this).datepicker
format: "yyyy-mm-dd"
weekStart: 1
autoclose: true
How can I get the data-behavior='datepicker' into the f.input line?
Thanks!
Try something like
#old hash syntax
:data => { :behavior => "datepicker" }
#new syntax
data: { behavior: "datepicker" }

Rails jquery datepicker date pre-loaded

I'm using jquery datepciker in a Rails app.
I would like to have the date already filled in when the form page loads. So, that the user can leave the date as-is or change it.
I was hoping the datepicker options would let me do that.
I tried this:
$("#comment_status_date").datepicker({
dateFormat: 'yy-mm-dd',
setDate: Date.now
});
But, the date field was empty.
Do I have to do some ruby code in the model or controller?
Does datepicker have an option to do this?
Thanks!!
I figured out a way:
<%= f.input :status_date, :as => :string, :label => 'Date', :class => 'calendar hasDatepicker', :input_html => { :value => Date.today.to_s } %>
I figured out a way:
<%= f.input :status_date, :as => :string, :label => 'Date', :class => 'calendar hasDatepicker', :input_html => { :value => Date.today.to_s } %>

nil :selected value for date helper in formtastic

I'm using the following code for a date field:
<%= f.input :date_of_birth, :selected => nil,
:order => [:day, :month, :year],
:prompt => {:day => "Day", :month => "Month", :year => "Year"},
:start_year => Time.now.year - 15,
:end_year => Time.now.year - 100 %>
Everything works as expected, except for :selected => nil. I'm using 0.9.7, all the specs pass, including the one regarding a nil :selected value. When I use this though, the current date is selected, save for the year, which doesn't exist.
Am I missing something here? I tried with :as => :date and that did't make any difference.
Tom
This is a known regression/bug in Formtastic right now. Expect a fix in the next gem release.
You could try to add :include_blank => true, maybe that helps?

Resources