将数据从 `datetime_select` 转换为 DateTime 对象的 Rails 方法在哪里? [英] Where is the Rails method that converts data from `datetime_select` into a DateTime object?

查看:38
本文介绍了将数据从 `datetime_select` 转换为 DateTime 对象的 Rails 方法在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在表单中使用 <%= f.datetime_select :somedate %> 时,它会生成如下 HTML:

When I use <%= f.datetime_select :somedate %> in a form, it generates HTML like:

<select id="some_date_1i" name="somedate1(1i)">  #year
<select id="some_date_2i" name="somedate1(2i)">  #month
<select id="some_date_3i" name="somedate1(3i)">  #day
<select id="some_date_4i" name="somedate1(4i)">  #hour
<select id="some_date_5i" name="somedate1(5i)">  #minute

提交该表单时,会收到 somedate1(i) 值:

When that form is submitted, the somedate1(<n>i) values are received:

{"date1(1i)"=>"2011", "date1(2i)"=>"2", "date1(3i)"=>"21", "date1(4i)"=>"19", "date1(5i)"=>"25"}

如何将其转换为 DateTime 对象?

How can I convert that into a DateTime object?

我可以编写自己的方法来执行此操作,但是由于 Rails 已经能够进行转换,我想知道是否可以调用该 Rails 方法来为我执行此操作?

I could write my own method to do this, but since Rails already is able to do the conversion, I was wondering if I could call that Rails method to do it for me?

我不知道去哪里找那个方法.

I don't know where to look for that method.

我最终是想解决如何处理日期/POST 参数中的次数?",这个问题是尝试找到其他问题的解决方案的第一步.

I'm ultimately trying to solve "How to handle date/times in POST parameters?" and this question is the first step in trying to find a solution to that other problem.

推荐答案

那个代码路径的开始,似乎就在这里:

The start of that code path, seems to be right about here:

https://github.com/rails/rails/blob/d90b4e2/activerecord/lib/active_record/base.rb#L1811

这很难找到!我希望这可以帮助您找到所需的内容

That was tricky to find! I hope this helps you find what you need

这篇关于将数据从 `datetime_select` 转换为 DateTime 对象的 Rails 方法在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆