Rails date_field not working in IE - ruby-on-rails

I have a search built on date fields. I want to use the date_field instead of date_selector if possible.
The date field works fine in chrome. However, in IE 11, the placeholder and controls do not work. The date select does work in IE.
<%= f.date_field( :req_resolution_from_date) %>
<%= f.date_select( :req_resolution_to_date) %>
In chrome, both date_field and date_select work.
However, in IE, only the date_select works
Is there any way to get the date_field to show in IE with the date controls?

Related

Rails Best In Place date initial date format

I'm really new to Rails and I'm using v6.1.4. In my application I'm using I18n localization. At other places for view-only label dates(not using BIP) I use
<%=l #client.active_date.to_date %>
and it works. However, in Best-In-Place there is a different syntax.
<span class="date-container"><%= best_in_place task, :due_date, :as => :date, class: 'hyperlink' %></span>
This causes the dates to be displayed in the yyyy-mm-dd format throughout. I want the dates to be formatted according to the locales currently in my application. I'm using the JQuery datepicker, but it is activated after I click on the date, and the format is changed to what I have set on the jquery setDefaults option.
How do I tackle this?
I think this post addresses your question.
You could add a display_as: :formatted_date to your method call and define that formatted_date method in your model. From there you should be able to format dates as needed.

Rails form flipping days and months

I have a form that received dates via two variable :start and :end. When I input the date they look right, but when I save, the model receive a date with the days and months flipped, and when the day is greater than 12, say 13th of March, it will simply show nil error.
My form field look like:
<div class="input-group date" id="datetimepicker4" data-target-input="nearest">
<%= f.text_field(:start, value: f.object.start ? f.object.start.strftime('%m/%d/%Y %l:%M %p') : nil, class: "form-control datetimepicker-input", data: {target: "#datetimepicker4"}, placeholder: "#{t :From}", required: true) %>
<div class="input-group-append" data-target="#datetimepicker4" data-toggle="datetimepicker">
<div class="input-group-text"><span class="fas fa-calendar-alt"></span></div>
</div>
</div>
Same for :end variable
The input form looks like :
The results looks like:
Looks like you try to use US date format, but rails is parsing submitted params with month and day switched. I am not sure if you would prefer to use US or international date format and I advise you to check how rails is handling parsing submitted params.
Also, have a look at defaults of calendar date time picker you use.
UPDATE.
I will try to provide more details. In one of my projects I am using a similar date time picker that looks like this
When submitted you will have the following params in our controller
{"date_time_picker_input_field"=>"2020-01-20"}.
The reason my date time picker formats date as YYYY-MM-DD is that in the view code I am using an option that is taken by datetimepicker code upon initialisation, something similar to
date_format: 'yyyy-mm-dd',
(this can be different in the date time picker you use and I suggested earlier to check documentation on options you can use).
I don't have to use strftime on date time provided to text_field, because date time JS code will format the date in format of your choice.

rails dyamically add fields to nested form

I have followed Ryans railscast #196 (revised) in order to dynamically add and remove fields to a nested form. It seems to be working just fine but I am having two issues.
I have 2 fields in the nested form (child area) that use the JQuery datepicker and as such have a class on them in the partial that displays the initial records. Initially the form is created with just 1 child. On this 1st child row the datepicker works just fine. However, when a new row is added the 2 fields with the datepicker no longer work. I believe the issue is due to the fact that when the new record gets created it does not assign the class to them, therefore the JS will not work :( No idea how to go about attempting to fix this, if it's even possible. If this is not possible then do I need to have a button to add a new record that will basically do this and refresh the form? I am hoping there's a way to do this dynamically since it just looks smoother.
When I go to edit the above record the same 2 fields are not showing the actual dates. The dates are fine and I can see that they are saved in the DB. I have another form that has a date field with the datepicker but that field is at the header and it is showing a value when editing the field. Obviously, the code I have to set the value of the date field is just not working. Again, would appreciate some help. The code is below for the partial view of these two fields.
<% fromdate_display = :fromdate %>
<%= f.text_field fromdate_display, placeholder: "mm/dd/yyyy", required: true, id: "price_fromdate", value: :fromdate.try(:strftime, "%m/%d/%Y") %>
<%= f.hidden_field :fromdate, id: "price_fromdatealt" %>
<% todate_display = :todate %>
<%= f.text_field todate_display, placeholder: "mm/dd/yyyy", required: true, id: "price_todate", value: :todate.try(:strftime, "%m/%d/%Y") %>
<%= f.hidden_field :todate, id: "price_todatealt" %>
I have tried changing the value: to be something as follows, but that throws an error:
value: #plan.plan_prices.fromdate.try(:strftime, "%m/%d/%Y")
Error message thrown:
undefined method `fromdate' for #<ActiveRecord::Associations::CollectionProxy::ActiveRecord_Associations_CollectionProxy_PlanPrice:0x36eb428>
I think the datepicker inputs dont work because are added dynamically after the datepicker plugin is loaded. Try to call the js plugin after you add a new input, maybe in the click event of link that add the inputs. Or try use this gem https://github.com/nathanvda/cocoon. By the way, what plugin you use for datepickers?

Styling the date fields of a Rails date_select

In Rails' forms, date select drop-downs are easy:
<%= f.label :my_date %>
<%= f.date_select :my_date %>
Then I get 3 select drop-downs for the year, month and day. Is there an easy way to add HTML between these select elements without using JavaScript? I want to be able to style and label them nicer.
Convert it to a text field and add a JQuery datepicker or similar.
Theres a good guide here, http://railscasts.com/episodes/213-calendars
You can easily style them with CSS as each part of date_select is postfixed with '_1i', '_2i' etc . For a date_select with id 'my_date' you can use
#my_date_1i // the year
#my_date_2i // the month
#my_date_3i // the day
Hope this helps
Cheers

Date is not saving in ruby on rails

I have added datepicker in _form.html.erb page to pick date at the time of creation. but when i click on create button it is showing field cannot be blank. when i see the params, it is showing "dob"=>"08/17/2012", but it is not passing the validation. I am not sure weather it is a format problem. can anyone help me to solve this problem. thnks
my application.html.erb file contains:-
<%= javascript_include_tag "jquery-1.8.0.min" %>
<%= javascript_include_tag "jquery-ui-1.8.23.custom.min" %>
<!--%= javascript_include_tag "application" %-->
<script>
$(function() {
$( "#datepicker" ).datepicker();
});
</script>
and _form.html.erb contains:-
<div class="span10 date-wid">
<%= f.text_field :dob, :id => 'datepicker' %>
</div>
It probably is your format. The correct format to save time to a database is this
For the date , March 1st, 2012 or 3/1/2012 :
20120301
So the order goes Year, Month, Day in the structure of 4 digits, 2 digits, 2 digits. Protip, you can add more which would be your hours minutes and seconds as well.
So make sure your datepicker is producing that as well :
$(".date-single-view").datepicker({
dateFormat: "yyyy-mm-dd"
});
That will add -, but it should still work. You can also do other things like have the datepicker on select drop the data format to a hidden input div, and the input field is just for show.
You can also use the Chronic gem to properly parse various different means of time.
https://github.com/mojombo/chronic

Resources