Search code examples
ruby-on-railsrubyformsdatelocaldate

how to change rails datetime_field defaults?


Me and my co-workers were testing my web app. And, in the form in my computer/local/dev and server/production it shows the date format dd/mm/yyyy 00:00 and for my coworkers mm/dd/yyyy 00:00 am/pm with the same local chrome settings and computer settings.

  • why is this?
  • and how can I change this for all users? if possible.

piece of code:

<div class="mini_jumbotron">
                <div class="mini_text">Inicio<%= image_tag("calendar.png", :class => "calendar")%>
                  </div> <br>
                </div>
              <%= f.datetime_field :Inicio %>

Solution

  • Try below code:

    <%= f.date_field :Inicio, as: :date, value: f.object.try(:strftime,"%d/%m/%Y"), class: 'form-control' %>
    

    You can also use this gem: https://github.com/Nerian/bootstrap-datepicker-rails on your gemfile.rb

    gem 'bootstrap-datepicker-rails'
    

    then bundle install and restart rails server

    then add this line to app/assets/stylesheets/application.css

    *= require bootstrap-datepicker
    

    and add this line to app/assets/javascripts/application.js

    //= require bootstrap-datepicker
    

    and to use this in your form:

    <%= f.date_field :Inicio, :id => "datepicker" %>
    

    and add this javascript on the same view of your form

    <script>
      $('#datepicker').datepicker({format: 'dd/mm/yyyy  HH:MM:ss'});
    </script>