Date format changing after reload page in rails 4 using datetimepicker

天涯浪子 提交于 2019-12-13 04:31:36

问题


I've noticed something strange in my app.

When I enter to the main page here: http://leszczyna.wzks.uj.edu.pl/12_stolarski/events_for_seniors/ and click on the second field in search and pick something, the date format appearing is 'dd-mm-yyyy'.

But when I click in the top left-hand corner 'Wydarzenia dla seniorów' date format is changing to 'dd/mm/yyyy'. I've noticed that locale is appearing in URL but have no idea if it has impact on that behavior.

What is more, when I refresh page after that, date format comes to the beginning.

What can cause that strange behavior of that input?

EDIT

Code from my view:

<div class="form-group">
      <label class="sr-only" for="date">Email</label>
      <%= text_field_tag :date,
                         params[:date],
                         placeholder: 'Kliknij, aby wybrać datę',
                         class: 'form-control input-lg date',
                         :data => {
                                 provide: 'datepicker'
                         }
      %>
    </div>

And my javascript:

    /* Search date */
if ($('body.homepage').length) {
    $('.date').datepicker({
        'language': "pl",
        'todayHighlight': true,
        'format': 'dd-mm-yyyy',
        'autoclose': true
    })
}

回答1:


It's happening due to turbolinks, turn off turbolinks for the top left-hand corner 'Wydarzenia dla seniorów' link. In the header your link is present under a div with navbar-header class. To that div element add data-no-turbolink, example below

<div class="navbar-header" data-no-turbolink>

This will resolve your problem!

or

In your js file, load javascript like this

ready = function(){

  /* Search date */
  if ($('body.homepage').length) {
    $('.date').datepicker({
        'language': "pl",
        'todayHighlight': true,
        'format': 'dd-mm-yyyy',
        'autoclose': true
    });
  }  
}
$(document).ready(ready);
$(document).on('page:load', ready);

This is the best solution.

Hope this helps!




回答2:


By adding following code in your form will resolve your problem.

<script type="text/javascript"> 
  $( document ).ready(function() { 
    $("#date").datepicker({ 
      'language': "pl",
      'todayHighlight': true,
      'format': 'dd-mm-yyyy',
      'autoclose': true
    }) 
  }); 
</script>


来源:https://stackoverflow.com/questions/30477864/date-format-changing-after-reload-page-in-rails-4-using-datetimepicker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!