datetime_select的多参数错误 [英] multiparameter error with datetime_select

查看:96
本文介绍了datetime_select的多参数错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表单中包含以下代码。

I have the following code in my form.

<%= f.datetime_select(:date_time, :prompt => {:day => 'Day', :month => 'Month', :year => 'Year'}, :start_year => Date.today.year, :end_year => Date.today.year + 2, :minute_step => 15, :include_blank => false) %> if either one is blank.

当其中一个字段留空时,我得到:

When one of the fields is left blank, I get:

1 error(s) on assignment of multiparameter attributes

有1个错误

正在传递的参数为:

The params that are being passed are:

{"utf8"=>"✓",
 "authenticity_token"=>"kQpfsj5RxnDtxkvBdwPEFnX1fY6euKnMQeDRAkvJvIE=",
 "event"=>{"description"=>"",
 "venue"=>"",
 "street"=>"",
 "city"=>"",
 "country_id"=>"",
 "date_time(1i)"=>"",
 "date_time(2i)"=>"",
 "date_time(3i)"=>"",
 "date_time(4i)"=>"00",
 "date_time(5i)"=>"00",
 "ticket_url"=>""},
 "x"=>"94",
 "y"=>"12"}

任何人都知道为什么会这样?

Anyone know why this is occurring?

链接,但是在Rails 3中也许有更好的解决方案?

There seems to be a "dirty" fix for this at this link, but perhaps there is a better solution in Rails 3?

推荐答案

基督徒。这是Rails中的一个错误,该错误会检查数据库以推断多参数属性所需的类型。我的猜测是您的 date_time属性与数据库中的时间列不相关。

Christian. This is a bug in Rails that checks the database to infer the type needed for the multiparameter attributes. My guess is that your "date_time" attribute is not associated with a time column in your database.

我最近解决了这个问题,我希望接受非数据库属性多参数属性,这是我能提出的最佳解决方案:

I recently tackled this problem where I wanted a non-database attribute to accepted multiparameter attributes, this was the best solution I could come up with:

我发现自己想设置一个 attr_accessor 使用 f.datetime_select 助手在 form_for 标记中处理将日期传递给我的模型。所以这就是我所拥有的:

I found myself wanting to set an attr_accessor to handle passing a date to my model in a form_for tag with the f.datetime_select helper. So this is what I had:

模型:

attr_accessor :my_time

查看:

<%= f.datetime_select :my_time %>

不幸的是,当我提交表格时,我得到了:

Unfortunately when I submit my form I get this:

1 error(s) on assignment of multiparameter attributes



<事实证明,这实际上是一个Rails错误,已经提交了票证。同时,我们如何进行这项工作?我发现唯一吸引人的解决方案是使用 composed_of 代替 attr_accessor 。所以...

型号:

  composed_of :my_time,
              :class_name => 'Time',
              :mapping => %w(Time to_s),
              :constructor => Proc.new{ |item| item },
              :converter => Proc.new{ |item| item }

我对 composed_of 方法,因此您应该对此进行自己的阅读,但是我知道的是,它为给定的实例变量创建了读取器和写入器,更重要的是,setter接受了多参数属性。我如何选择选项:

I know almost nothing about the composed_of method so you should probably do your own reading on it, but what I do know is that it creates both a reader and writer for the given instance variable, and more importantly, the setter accepts multiparameter attributes. How I chose the options:

class_name:我们期望的班级的名称。在这种情况下, Time

映射:第一个参数是类,第二个参数似乎可用于该类实例响应的任何方法至。我选择了 to_s
构造函数:不确定该如何工作。似乎在 @my_time nil 时被调用。

转换器:不确定如何做到这一点应该工作。从my_time =开始似乎被调用,但似乎不适用于批量分配。
此解决方案遇到的一个问题是,时间是在UTC而不是环境的时区中设定的。因此很遗憾,我们不能直接使用my_time,而是需要将其转换为正确的时区:

class_name: the name of our expected class. In this case, Time
mapping: the first argument is the class and the second argument seems to work with any method that an instance of the class responds to. I chose to_s constructor: Not really sure how this is supposed to work. Seems to be called when @my_time is nil.
converter: Not really sure how this is supposed to work. Seems to be called when from my_time=, but doesn't seem to be applied with mass assignment. One problem I ran into with this solution was that times were getting set in UTC instead of the environment's time zone. So unfortunately we cannot use my_time directly, but instead need to convert it to the proper time zone:

Time.zone.parse(my_time.to_s(:number))

这篇关于datetime_select的多参数错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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