T
T
The Whiz2013-07-31 18:31:51
Ruby on Rails
The Whiz, 2013-07-31 18:31:51

Date format in rails?

Railroaders, help. By design, my user using the datepicker ( jquery-ui ) selects the date,
JS

jQuery ->
  $('.datepicker').datepicker({ 
  	dateFormat: "dd.mm.y",
  	firstDay: 1
  });

New.html.erb
<p>
  	<%= f.label :start_date, "Начало" %><br>
  <%= f.text_field :start_date, class: "datepicker" %>
  </p>

Then this date is displayed in a variety of views. The problem is that rails outputs it in the format 0000-00-00, respectively, if you enter it in the form 02.07.13, the output is 0002-07-13.
I found a working variant with .strftime("%d %b. %Y") , but I would like to go deeper, and not drive this method against each date.
Where is it eradicated? I tried different crutches from StackOverflow, but nothing helped so far. Thanks

Answer the question

In order to leave comments, you need to log in

5 answer(s)
S
SeTeM, 2013-07-31
@SeTeM

You can rewrite the get method:

def start_date
  self[:start_date].strftime("%d %b. %Y") if self[:start_date]
end

M
Meliborn, 2013-07-31
@Meliborn

Helper or attr_accessor something like :start_date_human with strftime

M
Mike Grigorieff, 2013-07-31
@Grigorieff

In general, you can i18n.l do it through, and in the en.yml config specify the default date and time output format such as you need. And as far as I remember, in the config/environment.rb
file you can specify:

my_format = { :default => '%d %b. %Y' } 

ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.merge!(my_format) 

ActiveSupport::CoreExtensions::Date::Conversions::DATE_FORMATS.merge!(my_format)

J
jj_killer, 2013-08-01
@jj_killer

I used text_field with type date with fallback on jquery ui. And outside the input - time_tag formatting the content with js.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question