在我忘记之前,这是我正在运行的:Rails 3.2.4.rc1 Ruby 1.9.3 Formtastic 2.1(稳定)
我正在尝试使用 formtastic 来做用户信息表单。日期字段似乎无法正常工作。所有其他数据正在正确更新。看起来 PUT 请求没问题,但是当我查询我的数据库(mysql)时,它说日期 = NULL。
代码示例:
在我看来:
<%= semantic_form_for @author do |f| %>
<%= f.inputs do %>
<%= f.input :first_name %>
<%= f.input :last_name %>
<%= f.input :date_of_birth, :start_year => Time.now.year-500 %>
<%= f.input :nationality %>
<%= f.input :bio %>
<% end %>
<%= f.actions %>
<% end %>
PUT 请求:
Started PUT "/authors/4" for 127.0.0.1 at 2012-07-19 11:08:02 -0400
Processing by AuthorsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"UARYU7S0FTOLYU1Z04lZp/2Gn94DXiKuX/FN/DgYk/c=", "author"=>{"first_name"=>"Sheila", "last_name"=>"Wedigo", "date_of_birth(1i)"=>"1514", "date_of_birth(2i)"=>"3", "date_of_birth(3i)"=>"5", "nationality"=>"American", "bio"=>"Sheila is an awesome Great American Playwright"}, "commit"=>"Update Author", "id"=>"4"}
ETA:这是控制台中的 MYSQL:
Processing by AuthorsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"gjSXdsSWPBzL65fOHX/pe3XTFRau4NYzZSYsyp1aPAs=", "author"=>{"first_name"=>"Stinky ", "last_name"=>"mggee", "date_of_birth(1i)"=>"2010", "date_of_birth(2i)"=>"4", "date_of_birth(3i)"=>"4", "nationality"=>"Finnish", "bio"=>"TEST"}, "commit"=>"Create Author"}
SQL (0.2ms) BEGIN
SQL (154.1ms) INSERT INTO `authors` (`bio`, `created_at`, `date_of_birth`, `date_of_death`, `first_name`, `last_name`, `nationality`, `updated_at`) VALUES (?, ?, ?, ?, ?, ?, ?, ?) [["bio", "TEST"], ["created_at", Tue, 24 Jul 2012 13:30:23 EDT -04:00], ["date_of_birth", nil], ["date_of_death", nil], ["first_name", "Stinky "], ["last_name", "mggee"], ["nationality", "Finnish"], ["updated_at", Tue, 24 Jul 2012 13:30:23 EDT -04:00]]
当我询问 mysql 时的输出:
mysql> select date_of_birth from authors where id = 4;
+---------------+
| date_of_birth |
+---------------+
| NULL |
+---------------+
1 row in set (0.00 sec)
我怀疑这可能与RoR Guides中的以下信息有关:“select_date 不适用于更新或创建 Active Record 对象的表单,因为 Active Record 期望 params 散列的每个元素对应一个属性。模型对象日期和时间的助手提交具有特殊名称的参数,当 Active Record 看到具有此类名称的参数时,它知道它们必须与其他参数组合并提供给适合列类型的构造函数。”
也就是说,我在互联网上没有看到其他人遇到这个问题,而且这似乎是 Formtastic 会处理的那种事情。在我最近从 Rails 2 升级到 Rails 3 之前,这可以正常工作。