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 -- Posted via http://www.ruby-forum.com/.
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 <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>:> > 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 > -- > Posted via http://www.ruby-forum.com/. > > > >-- Von meinen Mobilgerät aus gesendet
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 >-- Posted via http://www.ruby-forum.com/.
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, > > HermanThis 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 -- Posted via http://www.ruby-forum.com/.
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 -- Posted via http://www.ruby-forum.com/.
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 > > HermanYes. 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 -- Posted via http://www.ruby-forum.com/.