简体   繁体   English

ActiveRecord :: MultiparameterAssignmentErrors是什么意思?

[英]What Does ActiveRecord::MultiparameterAssignmentErrors Mean?

I have a rails form with a datetime_select field. 我有一个带有datetime_select字段的rails表单。 When I try to submit the form, I get the following exception: 当我尝试提交表单时,我得到以下异常:

ActiveRecord::MultiparameterAssignmentErrors in WidgetsController#update 
1 error(s) on assignment of multiparameter attributes

If it's a validation error, why don't I see an error on the page? 如果是验证错误,为什么我在页面上看不到错误?

This is in Rails 2.0.2 这是在Rails 2.0.2中

It turns out that rails uses something called Multi-parameter assignment to transmit dates and times in small parts that are reassembled when you assign params to the model instance. 事实证明,rails使用称为多参数分配的东西来传输日期和时间,这些小部件在将params分配给模型实例时会重新组装。

My problem was that I was using a datetime_select form field for a date model field. 我的问题是我使用datetime_select表单字段作为日期模型字段。 It apparently chokes when the multi-parameter magic tries to set the time on a Date object. 当多参数魔法试图在Date对象上设置时间时,它显然会窒息。

The solution was to use a date_select form field rather than a datetime_select . 解决方案是使用date_select表单字段而不是datetime_select

Super hack, but I needed to solve this problem right away for a client project. 超级黑客,但我需要立即解决这个问题的客户端项目。 It's still a bug with Rails 2.3.5. 它仍然是Rails 2.3.5的一个错误。

Using either date_select or datetime_select , if you add this to your model in the initialize method, you can pre-parse the passed form-serialized attributes to make it work: 使用date_selectdatetime_select ,如果在initialize方法中将其添加到模型中,则可以预先解析传递的form-serialized属性以使其工作:

def initialize(attributes={})
  date_hack(attributes, "deliver_date")
  super(attributes)
end

def date_hack(attributes, property)
  keys, values = [], []
  attributes.each_key {|k| keys << k if k =~ /#{property}/ }.sort
  keys.each { |k| values << attributes[k]; attributes.delete(k); }
  attributes[property] = values.join("-")
end

I am using this with a nested, polymorphic, model. 我使用嵌套的多态模型。 Here's a question I had showing the models I'm using . 这是我展示我正在使用的模型的问题 So I needed accepts_nested_attributes_for with a datetime. 所以我需要accepts_nested_attributes_for和datetime。

Here's the input and output using the console: 这是使用控制台的输入和输出:

e = Event.last
=> #<Event id: 1052158304 ...>
e.model_surveys
=> []
e.model_surveys_attributes = [{"survey_id"=>"864743981", "deliver_date(1i)"=>"2010", "deliver_date(2i)"=>"2", "deliver_date(3i)"=>"11"}]
PRE ATTRIBUTES: {"survey_id"=>"864743981", "deliver_date(1i)"=>"2010", "deliver_date(2i)"=>"2", "deliver_date(3i)"=>"11"}
# run date_hack
POST ATTRIBUTES: {"survey_id"=>"864743981", "deliver_date"=>"2010-2-11"}
e.model_surveys
=> [#<ModelSurvey id: 121, ..., deliver_date: "2010-02-11 05:00:00">]
>> e.model_surveys.last.deliver_date.class
=> ActiveSupport::TimeWithZone

Otherwise it was either null, or it would throw the error: 否则它是null,否则会抛出错误:

1 error(s) on assignment of multiparameter attributes

Hope that helps, Lance 希望有所帮助,兰斯

This is not a bug in Rails, it is the intended behavior of the multi-parameter attribute writer. 这不是Rails中的错误,它是多参数属性编写器的预期行为。 I'm willing to bet that the original poster's deliver_date field in the database is a varchar as opposed to a date or datetime type. 我愿意打赌,数据库中原始海报的deliver_date字段是varchar,而不是日期或日期时间类型。 ActiveRecord uses each part of the multi-parameter attribute to send to the new method of the serialized type. ActiveRecord使用多参数属性的每个部分发送到序列化类型的新方法。 The number 1, 2, 3, etc indicates the constructor parameter position and the "i" tells ActiveRecord to call to_i on the parameter before passing it to the constructor. 数字1,2,3等表示构造函数参数位置,“i”告诉ActiveRecord在将参数传递给构造函数之前调用参数上的to_i。 In this case they are all "i's" because DateTime.new(year, month, day) expects three Integers not three Strings. 在这种情况下,它们都是“我”,因为DateTime.new(年,月,日)需要三个整数而不是三个字符串。

If the deliver_date column in the database isn't a type that's serialized to a DateTime then ActiveRecord will throw a ActiveRecord::MultiparameterAssignmentErrors exception because String.new(2010,2,11) won't be successful. 如果数据库中的deliver_date列不是序列化为DateTime的类型,则ActiveRecord将抛出ActiveRecord :: MultiparameterAssignmentErrors异常,因为String.new(2010,2,11)将不会成功。

Source: https://github.com/rails/rails/blob/v3.0.4/activerecord/lib/active_record/base.rb#L1739 资料来源: https//github.com/rails/rails/blob/v3.0.4/activerecord/lib/active_record/base.rb#L1739

ActiveRecord throws the MultiparameterAssignmentErrors exception when you try to set an invalid date to a models attribute. 当您尝试将无效日期设置为models属性时,ActiveRecord会引发MultiparameterAssignmentErrors异常。

Try to pick a Nov 31 date from the date_select or datetime_select dropdown and you will get this error. 尝试从date_select或datetime_select下拉列表中选择11月31日的日期,您将收到此错误。

Like Zubin I've seen this exception when the form submits a month as a month name rather than a numerical month string (eg. October rather than 10). 和Zubin一样,当表单提交月份作为月份名称而不是数字月份字符串(例如10月而不是10)时,我看到了这种异常。

One user agent I've encountered seems to submit the contents of the option tag rather than the value attribute: 我遇到的一个用户代理似乎提交了option标签的内容而不是value属性:

Mozilla/5.0 (SymbianOS/9.2; U; Series60/3.1 NokiaE66-1/300.21.012; Profile/MIDP-2.0 Configuration/CLDC-1.1 ) AppleWebKit/413 (KHTML, like Gecko) Safari/413 Mozilla / 5.0(SymbianOS / 9.2; U; Series60 / 3.1 NokiaE66-1 / 300.21.012; Profile / MIDP-2.0配置/ CLDC-1.1)AppleWebKit / 413(KHTML,类似Gecko)Safari / 413

So in the case of submitting a multi-parameter date from a helper generated select (from date_select helper) your params will have: 因此,如果从帮助程序生成的select(from date_select helper)提交多参数日期,则params将具有:

"event"=> {
    "start_on(2i)"=>"October",
    "start_on(3i)"=>"19",
    "start_on(1i)"=>"2010"
}

This creates an exception: ActiveRecord::MultiparameterAssignmentErrors: 1 error(s) on assignment of multiparameter attributes 这会创建一个异常: ActiveRecord :: MultiparameterAssignmentErrors:分配多参数属性时发生1个错误

Most user agents will correctly submit: 大多数用户代理将正确提交:

"event"=> {
    "start_on(2i)"=>"10",
    "start_on(3i)"=>"19",
    "start_on(1i)"=>"2010"
}

This error can also occur with webrat/cucumber when filling in form data using a table. 使用表填写表单数据时,webrat / cucumber也会发生此错误。

eg this doesn't work: 例如,这不起作用:

When I fill in the following:
  | report_from_1i | 2010     |
  | report_from_2i | January  |
  | report_from_3i | 1        |
  | report_to_1i   | 2010     |
  | report_to_2i   | February |
  | report_to_3i   | 1        |

but this does: 但这样做:

When I fill in the following:
  | report_from_1i | 2010 |
  | report_from_2i | 1    |
  | report_from_3i | 1    |
  | report_to_1i   | 2010 |
  | report_to_2i   | 2    |
  | report_to_3i   | 1    |

In my case the ActiveRecord am/pm plugin caused the error through an incorrect alias_method_chain resulting in an StackLevelTooDeep exception. 在我的情况下,ActiveRecord am / pm插件通过不正确的alias_method_chain导致错误,导致StackLevelTooDeep异常。

The plugin was included by the unobtrusive_date_picker plugin. 该插件包含在unobtrusive_date_picker插件中。

The look into this before hacking away. 在黑客攻击之前看看这个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM