Trouble in storing dates from localized form text fields

[Rails 3.1.3 / ruby 1.9.2 ]

I am trying to manage localized dates and I don’t actually understand
how it’s managed w Rails 3.1 … I thought there was no more issues w
I18n…

my locale is defined : I18n.locale => :fr
I am using a localized version of jQuery DatePicker , so it displays
selected dates in the text field like : 01/02/2012 (Feb first )
i.e. ( in generated html):
<inputid="start_shooting_date"class="text
hasDatepicker"type="text"value="01/02/2012"name=“project[start_shooting_date]”>

in my Project model , date validations are OK, then I store the
updated project
@project.update_attributes(params[:project])

and I get stored in the DB : start_shooting_date: “2012-01-02
00:00:00” (Jan 2nd) rather than “2012-02-01 00:00:00” ( Feb
first )

I’ll appreciated your feedback …

I defined in environment.rb ( don’t know if necessary… for input .
but it seems to be for output, or is it redundant with defaults .yml
files ?)

if locale french

Date::DATE_FORMATS.merge!( :default => “%d/%m/%Y” )

I also defined the I18n default format in the .yml files

fr:
date:
formats:
default: “%d/%m/%Y”
short: “%e %b”
long: “%e %B %Y”
full: “%A %d %B %Y”
time:
formats:
default: “%d %B %Y %H:%M:%S”
short: “%d %b %H:%M”
long: “%A %d %B %Y %H:%M”
am: ‘am’
pm: ‘pm’

en:
date:
formats:
default: “%Y-%m-%d”
short: “%b %d”
long: “%B %d, %Y”
full: “%A %B %d, %Y”
time:
formats:
default: “%a, %d %b %Y %H:%M:%S %z”
short: “%d %b %H:%M”
long: “%B %d, %Y %H:%M”
am: “am”
pm: “pm”

[SOLVED]

don’t know if it’s the best solution, but it worked…
changed the date text_fields to text_field_tag, on output , set the
value in the controller, with I18n and local format

@start_shooting_date = I18n.l( @project[:start_shooting_date], :format =>
“%d/%m/%Y”)

then on input , set the attribute value from the form field in the
controller, with I18n and local format

@project = Project.new(params[:project]) # on create
@project.attributes = params[:project] # on update
@project[:start_shooting_date] = I18n.l(params[:start_shooting_date].to_date,
:format => “%d/%m/%Y”)

if @project.save
this way, the dates are correctly saved in the DB with the
standard :db format but input/output in the form is in local format
“dd/mm/yyyy” ( also defined like this in localized DatePicker

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs