How to save it..?

已查看 21 次
跳至第一个未读帖子

ina

未读,
2007年11月23日 09:49:022007/11/23
收件人 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

未读,
2007年11月23日 11:39:532007/11/23
收件人 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

未读,
2007年11月25日 08:36:482007/11/25
收件人 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

未读,
2007年11月25日 11:28:172007/11/25
收件人 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

未读,
2007年11月25日 11:29:012007/11/25
收件人 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

未读,
2007年11月26日 04:34:252007/11/26
收件人 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

未读,
2007年11月26日 04:41:242007/11/26
收件人 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

未读,
2007年11月27日 16:58:502007/11/27
收件人 calendar_date_select
Many thanks for your help - this is just what I needed. Thanks for the
great plugin too, Tim.

Tim Harper

未读,
2007年11月27日 17:07:522007/11/27
收件人 calendar_d...@googlegroups.com
Excellent, glad you enjoy it

Tim
回复全部
回复作者
转发
0 个新帖子