ActiveRecord::MultiparameterAssignmentErrors 是什么意思?

发布于 2024-07-05 18:09:11 字数 276 浏览 10 评论 0原文

我有一个带有 datetime_select 字段的 Rails 表单。 当我尝试提交表单时,出现以下异常:

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

如果是验证错误,为什么我在页面上看不到错误?

这是在 Rails 2.0.2 中

I have a rails form with a datetime_select field. 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

守望孤独 2024-07-12 18:09:11

事实证明,rails 使用一种称为多参数分配的方法来传输小部分中的日期和时间,当您将参数分配给模型实例时,这些小部分会重新组装。

我的问题是我使用 datetime_select 表单字段作为日期模型字段。 当多参数魔法尝试在 Date 对象上设置时间时,它显然会窒息。

解决方案是使用 date_select 表单字段而不是 datetime_select

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.

My problem was that I was using a datetime_select form field for a date model field. It apparently chokes when the multi-parameter magic tries to set the time on a Date object.

The solution was to use a date_select form field rather than a datetime_select.

漫雪独思 2024-07-12 18:09:11

超级黑客,但我需要立即为客户项目解决这个问题。 在 Rails 2.3.5 中这仍然是一个错误。

使用 date_selectdatetime_select,如果您在 initialize 方法中将其添加到模型中,则可以预先解析传递的表单序列化属性使其工作:

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

我将其与嵌套的多态模型一起使用。 这是我提出的一个问题,展示了我正在使用的模型。 所以我需要带有日期时间的 accepts_nested_attributes_for

这是使用控制台的输入和输出:

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

否则它要么为空,要么会抛出错误:

多参数属性分配时出现 1 个错误

希望有所帮助,

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.

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:

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.

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:

1 error(s) on assignment of multiparameter attributes

Hope that helps,
Lance

不即不离 2024-07-12 18:09:11

这不是 Rails 中的错误,而是多参数属性编写器的预期行为。 我敢打赌,数据库中原始发布者的 Deliver_date 字段是 varchar,而不是日期或日期时间类型。 ActiveRecord 使用多参数属性的每个部分发送到序列化类型的新方法。 数字 1、2、3 等表示构造函数参数位置,“i”告诉 ActiveRecord 在将参数传递给构造函数之前调用 to_i。 在本例中,它们都是“i”,因为 DateTime.new(year,month,day) 需要三个整数而不是三个字符串。

如果数据库中的 Deliver_date 列不是序列化为 DateTime 的类型,则 ActiveRecord 将引发 ActiveRecord::MultiparameterAssignmentErrors 异常,因为 String.new(2010,2,11) 将不会成功。

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

This is not a bug in Rails, it is the intended behavior of the multi-parameter attribute writer. 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. ActiveRecord uses each part of the multi-parameter attribute to send to the new method of the serialized type. 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. In this case they are all "i's" because DateTime.new(year, month, day) expects three Integers not three Strings.

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.

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

戈亓 2024-07-12 18:09:11

当您尝试为模型属性设置无效日期时,ActiveRecord 会引发 MultiparameterAssignmentErrors 异常。

尝试从 date_select 或 datetime_select 下拉列表中选择 11 月 31 日,您将收到此错误。

ActiveRecord throws the MultiparameterAssignmentErrors exception when you try to set an invalid date to a models attribute.

Try to pick a Nov 31 date from the date_select or datetime_select dropdown and you will get this error.

通知家属抬走 2024-07-12 18:09:11

像 Zubin 一样,当表单提交月份作为月份名称而不是数字月份字符串(例如,十月而不是 10)时,我就看到了此异常。

我遇到的一个用户代理似乎提交了选项标记的内容而不是值属性:

Mozilla/5.0(SymbianOS/9.2;U;
Series60/3.1 诺基亚E66-1/300.21.012;
简介/MIDP-2.0
配置/CLDC-1.1)
AppleWebKit/413(KHTML,如 Gecko)
Safari/413

因此,在从助手生成的选择(来自 date_select 助手)提交多参数日期的情况下,您的参数将具有:

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

这会创建一个异常:ActiveRecord::MultiparameterAssignmentErrors: 1 error(s) on assignment多参数属性

大多数用户代理都会正确提交:

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

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).

One user agent I've encountered seems to submit the contents of the option tag rather than the value attribute:

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

So in the case of submitting a multi-parameter date from a helper generated select (from date_select helper) your params will have:

"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

Most user agents will correctly submit:

"event"=> {
    "start_on(2i)"=>"10",
    "start_on(3i)"=>"19",
    "start_on(1i)"=>"2010"
}
温折酒 2024-07-12 18:09:11

使用表格填写表单数据时,webrat/cucumber 也会出现此错误。

例如,这不起作用:

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        |

但这确实有效:

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    |

This error can also occur with webrat/cucumber when filling in form data using a table.

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    |
无可置疑 2024-07-12 18:09:11

就我而言,ActiveRecord am/pm 插件通过不正确的 alias_method_chain 导致错误,从而导致 StackLevelTooDeep 异常。

该插件包含在 unobtrusive_date_picker 插件中。

在破解之前先看看这个。

In my case the ActiveRecord am/pm plugin caused the error through an incorrect alias_method_chain resulting in an StackLevelTooDeep exception.

The plugin was included by the unobtrusive_date_picker plugin.

The look into this before hacking away.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文