Forum: Ruby on Rails how to save german date text_field in database

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Herman M. (Guest)
on 2009-05-26 11:48
Hi Ruby Comunity!

To display a database stored date like %Y-%m-%d  into a text_field with
german format %d.%m.Y. is not so difficult. For conversion you can do
something like

<%= f.text_field :date_test, :value =>
@tbl_datum.date_test.strftime('%d.%m.%Y')%>

or use a helper.

But how can I save it from the form back INTO the database.

I tried it like in Rubycast Nr. 32 "time in textfield" but I think it's
not the same because you have to convert the date first from %d.%m.%Y to
database format%Y-%m-%d.

I tried many (3-days long), but nothings work.

Can somebody help me please, I'm really desperate:-(

Thanks ahead for every proposal!

Regs,

Herman
mike (Guest)
on 2009-05-26 12:08
(Received via mailing list)
Hallo Hermann,
Try to convert the text field param to a Date Object using the various
parsing options provided with the Date class. Then save this to the
database.
Greetings, mike

2009/5/26, Herman Müller <removed_email_address@domain.invalid>:
> or use a helper.
>
> Thanks ahead for every proposal!
>
> Regs,
>
> Herman
> --
> Posted via http://www.ruby-forum.com/.
>
> >
>

--
Von meinen Mobilgerät aus gesendet
Herman M. (Guest)
on 2009-05-26 12:31
Hi Mike,

thanks for your rapid answer.

Can you give me an example how to do this?

Thanks in front!

Regs

Herman


mike wrote:
> Hallo Hermann,
> Try to convert the text field param to a Date Object using the various
> parsing options provided with the Date class. Then save this to the
> database.
> Greetings, mike
>
Damjan R. (Guest)
on 2009-05-26 14:23
Herman Müller wrote:
> Hi Ruby Comunity!
>
> To display a database stored date like %Y-%m-%d  into a text_field with
> german format %d.%m.Y. is not so difficult. For conversion you can do
> something like
>
> <%= f.text_field :date_test, :value =>
> @tbl_datum.date_test.strftime('%d.%m.%Y')%>
>
> or use a helper.
>
> But how can I save it from the form back INTO the database.
>
> I tried it like in Rubycast Nr. 32 "time in textfield" but I think it's
> not the same because you have to convert the date first from %d.%m.%Y to
> database format%Y-%m-%d.
>
> I tried many (3-days long), but nothings work.
>
> Can somebody help me please, I'm really desperate:-(
>
> Thanks ahead for every proposal!
>
> Regs,
>
> Herman

This is how I do it. Unless until I digg into i18n

Put into application.rb

def update_date(*args)
  rec = args.shift
  args.each do |e|
    d,m,y,time = params[rec][e].split(/\.| /)
    params[rec][e] = y + '-' + m + '-' + d + ' ' + time unless y.nil?
  end
end

Then call:
  update_date(:doc, :time_created, :time_closed)

before you update attributes in your controller.

by
TheR
Herman M. (Guest)
on 2009-05-26 16:04
Hi Damjan,
Hi Mike,

I made a helper according to the Railscast Nr. 32 "time in textfield"
and played on Mikes advice a little bit with the Date Object and hurray
it works.

 def date_test_string
    date_test.strftime('%d.%m.%Y')
  end

  def date_test_string=(date_test_str)
    self.date_test = Date.strptime(date_test_str, '%d.%m.%Y')
  rescue ArgumentError
  @date_test_invalid = true

  end

  def validate
    errors.add(:date_test, "is invalid") if @date_test_invalid
  end

@Damjan:

The example you wrote, what you mean with:

update_date(:doc, :time_created, :time_closed)etc.

are that the date-attributes inside the model

like <%= f.text_field :date_test %> ?

Regs

Herman
Damjan R. (Guest)
on 2009-05-27 13:07
Herman Müller wrote:
> Hi Damjan,
> Hi Mike,
>
> I made a helper according to the Railscast Nr. 32 "time in textfield"
> and played on Mikes advice a little bit with the Date Object and hurray
> it works.
>
>  def date_test_string
>     date_test.strftime('%d.%m.%Y')
>   end
>
>   def date_test_string=(date_test_str)
>     self.date_test = Date.strptime(date_test_str, '%d.%m.%Y')
>   rescue ArgumentError
>   @date_test_invalid = true
>
>   end
>
>   def validate
>     errors.add(:date_test, "is invalid") if @date_test_invalid
>   end
>
> @Damjan:
>
> The example you wrote, what you mean with:
>
> update_date(:doc, :time_created, :time_closed)etc.
>
> are that the date-attributes inside the model
>
> like <%= f.text_field :date_test %> ?
>
> Regs
>
> Herman

Yes. I prefere:
<%= text_field :doc, :date_test %>
or in my case I use calendar_date_select
<%= calendar_date_select :doc, :time_closed, :time => true, :size => 15
%>

update_date method will update params[:doc][:time_created] and
params[:doc][:time_closed] fields.

You call it before updating fields in a model with values obtained from
browser.

def save
  @doc = Doc.find(params[:id])
  update_date(:doc, :time_created, :time_closed)
  if @doc.update_attributes(params[:doc])
   ...
   ...
end


by
TheR
This topic is locked and can not be replied to.