How to save it..?

21 views
Skip to first unread message

ina

unread,
Nov 23, 2007, 9:49:02 AM11/23/07
to calendar_date_select
Hi everybody,
first of all: great piece of code, thank you!

Now the very basic question: How do I save the date?

I have a model with a date-field, but when I pass the form values to
saving, I am passing the (finnish formatted) string
("von"=>"17.11.2007") to the controller, so where and how am I
converting it back into a rails date format?

thanks,
ina

Tim Harper

unread,
Nov 23, 2007, 11:39:53 AM11/23/07
to calendar_d...@googlegroups.com
It looks like ruby does not support that format natively.  You'll need to alias chain on Date::_parse to intercept a finish string and translate it.

tim

Gregor Schmidt

unread,
Nov 25, 2007, 8:36:48 AM11/25/07
to calendar_date_select
Hello,

I had the same problem and came up with following piece of code.
Perhaps this a helper for anybody. Feel free to comment on it, I'm not
sure if I made it as fast and simple as possible.

class Date
class << self
def _parse_with_finnish_format(date, now = Time.now)
if (match_data = date.strip.match(/^(\d{1,2})\.(\d{1,2})\.
(\d{4})$/))
{:mday => match_data[1].to_i,
:mon => match_data[2].to_i,
:year => match_data[3].to_i}
else
_parse_without_finnish_format(date, now)
end
end
alias_method_chain :_parse, :finnish_format
end
end


Thanks for the nice plugin, by the way.

Cheers,

Gregor

On Nov 23, 5:39 pm, "Tim Harper" <timchar...@gmail.com> wrote:
> It looks like ruby does not support that format natively. You'll need to
> alias chain on Date::_parse to intercept a finish string and translate it.
>
> tim
>

Tim Harper

unread,
Nov 25, 2007, 11:28:17 AM11/25/07
to calendar_d...@googlegroups.com
This is fantastic Gregor!  It's missing the time part, but that could be added with not much difficulty.

Tim

Tim Harper

unread,
Nov 25, 2007, 11:29:01 AM11/25/07
to calendar_d...@googlegroups.com
It would be great if the Calendar Date Select plugin automatically hooked into the server-side Date parser and added parsing format as needed according to the current selected format.

Chris Bartlett

unread,
Nov 26, 2007, 4:34:25 AM11/26/07
to calendar_date_select
This code looks very handy, thanks, but where would I put it (which
file)? I need to get British-style date formatting (31/12/2007)
working with MySQL (2007-12-31) and can't believe it is so much work!

Thanks,
Chris

Gregor Schmidt

unread,
Nov 26, 2007, 4:41:24 AM11/26/07
to calendar_d...@googlegroups.com
Hi Chris,

> This code looks very handy, thanks, but where would I put it (which
> file)? I need to get British-style date formatting (31/12/2007)
> working with MySQL (2007-12-31) and can't believe it is so much work!

I put it in config/initializers/calendar_date_select.rb

But this place works only on EdgeRails or Rails 2.0. All others will
put it at the buttom of config/environment.rb

Cheers,

Gregor

Chris Bartlett

unread,
Nov 27, 2007, 4:58:50 PM11/27/07
to calendar_date_select
Many thanks for your help - this is just what I needed. Thanks for the
great plugin too, Tim.

Tim Harper

unread,
Nov 27, 2007, 5:07:52 PM11/27/07
to calendar_d...@googlegroups.com
Excellent, glad you enjoy it

Tim
Reply all
Reply to author
Forward
0 new messages