ruby on rails多参数属性*真的*工作(datetime_select) [英] How do ruby on rails multi parameter attributes *really* work (datetime_select)

查看:103
本文介绍了ruby on rails多参数属性*真的*工作(datetime_select)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相信 datetime_select 是黑魔法。我真正想弄清楚的是整个 1i 2i 3i 4i ...多个参数的东西。具体来说,它在后端如何处理(activerecord,别的什么?)订单号后面的'i'是什么?它是一个类型说明符?如果有,可以使用其他类型的?我已经阅读了date_helper.rb的源码,这是非常不透明的。

I believe that datetime_select is black magic. What I'm really trying to figure out is the whole 1i,2i,3i,4i... multiple parameters stuff. Specifically how is it handled in the back end (activerecord, something else?). What's with the 'i' after the order-number? Is it a type-specifier? If so what are other types that are available? I've read the source of date_helper.rb and it's quite opaque.

这是我的动机:

我的模型中有一个:datetime 列,我想通过两个 text_field s在一个视图中输入:时间和日期。他们需要验证,合并在一起,然后存储到datetime列。最终我将使用JavaScript日历在日期字段中输入日期。

I've got a :datetime column in my model and I want to input in the view via two text_fields: one for date and one for time. They need to be validated, merged together, then stored into the datetime column. Ultimately I'll be using a javascript calendar to input dates into the date field.

有没有人这样做?我尝试使用虚拟属性(除了基本轨迹之外,非常无证据),问题是当创建一个新的activerecord对象并且具有nil属性时,虚拟属性失败(未定义的方法 strftime 对于nil类,这是有道理的)。

So has anybody done this? I tried using virtual attributes (incredibly undocumented besides the rudimentary railscast) and the issue was that when a new activerecord object is created and has nil attributes, the virtual attribute fails (undefined method strftime for nil class, which makes sense).

任何人都有任何建议或最佳做法?谢谢!

Anybody have any suggestions or best practices? Thanks!

推荐答案

我有同样的问题。这是我发现的...

I had the same question. Here's what I found...

http:// apidock。 com / rails / ActiveRecord / Base / assign_multiparameter_attributes


assign_multiparameter_attributes(pairs)
private

assign_multiparameter_attributes(pairs) private

为需要多个
构造函数参数的所有属性
类实例化对象。这是由
调用新的列类型或
聚合类型(通过composition_of)
对象与这些参数。所以
有对write_on(1)=
2004,written_on(2)=6,
written_on(3)=24将实例化
written_on (日期类型)与
Date.new(2004,6,24)。您可以
还在
中指定一个类型转换字符的括号,以便在
构造函数中使用参数
。使用我为Fixnum,f为
Float,s为String,a为数组。
如果给定的
属性的所有值都为空,则属性
将被设置为nil。

Instantiates objects for all attribute classes that needs more than one constructor parameter. This is done by calling new on the column type or aggregation type (through composed_of) object with these parameters. So having the pairs written_on(1) = "2004", written_on(2) = "6", written_on(3) = "24", will instantiate written_on (a date type) with Date.new("2004", "6", "24"). You can also specify a typecast character in the parentheses to have the parameters typecasted before they’re used in the constructor. Use i for Fixnum, f for Float, s for String, and a for Array. If all the values for a given attribute are empty, the attribute will be set to nil.

所以这些数字是列的数据类型的参数。 i是将其转换为整数。支持其他类型,如浮点数f等。

So the numbers are for the parameters of the data type that the column is. The "i" is to convert it to an integer. Other types are supported like "f" for float, etc.

您可以在 execute_callstack_for_multiparameter_attributes ,它检测到目标类型并实例化它传递的值。

You can see here in execute_callstack_for_multiparameter_attributes that it detects the target type and instantiates it passing the values.

所以,要直接回答提出的问题,我不要以为可以使用它来覆盖DateTime的创建方式,因为它使用不支持您要传递的格式的构造函数。我的解决方法是将DateTime列分割为一个Date和One Time列。那么UI可以按照我想要的方式工作。

So, to try directly answering the posed question, I don't think you can use it to override how a DateTime is created because it uses the constructor which doesn't support the format you want to pass in. My work-around was to split the DateTime column out to one Date and one Time column. Then the UI could work the way I wanted.

我想知道是否可能分别在代表日期和时间的类上创建属性访问器,但保留一个DateTime列。然后,表单可以引用这些单独的访问器。这可能是可行的。另一个选择可能是使用 composition_of 进一步自定义类型。

I wonder if it might be possible to create attribute accessors on the class that represent the date and time separately but keep a single DateTime column. Then the form could reference those separate accessors. That might be workable. Another option might be to use composed_of to customize the type further.

希望能帮助别人。 :)

Hope that helps someone else. :)

这篇关于ruby on rails多参数属性*真的*工作(datetime_select)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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