嵌套模型、表单和 date_select FormHelper 集成
我遵循了 Ryan Bates 嵌套模型教程。我的几个嵌套模型都有与之关联的日期。在我的迁移中,它们实际上是“日期”类型。
我尝试过的一些事情和遇到的问题
- date_select - 可以处理表单对象前缀,但不能处理嵌套模型属性
- select_year - 不适用于表单对象
- a通过使用 (Time.now.year - 100)..(Time.now.year) 并覆盖 attr 访问器 start_date 和 end_date 以获取选择中的值来形成日期并将其传回,常规选择填充年份。 仅适用于创建,不适用于更新
- 将字段的数据类型更改为字符串,并使用 (Time.now.year - 100)..(Time. now.year)有效,但在编辑时,它不会使用当前信息重新填充选择
任何想法或提示都会有帮助。
编辑:before_save似乎更有希望,但由于某种原因,该值在保存之前为零,但在 日志转储。
编辑2:有趣的是,这似乎只是“更新”时的问题,而不是“创建”时的问题。
I have followed Ryan Bates tutorial on nested models. Several of my nested models have dates associated with them. In my migrations, they are actually the type "Date."
Some things I have tried and problems I've run into
- date_select - can handle the form object prefix, but not nested models attributes
- select_year - doesn't work with form object
- a regular select populated with the year by using (Time.now.year - 100)..(Time.now.year) and overriding the attr accessor start_date and end_date to take the value in the select to form a date and passing that back. works on create only, not on update
- changing the data type of the field to string and using a regular select populated with the year by using using (Time.now.year - 100)..(Time.now.year) works, but on edit, it won't repopulate the select with the current information
Any ideas or hints would be helpful.
Edit: before_save seems to be more promising but for some reason, the value is nil coming into before save but is visible in the log dump.
Edit 2: Interestingly, this only seems to be a problem on 'update', not on 'create'.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这就是解决方案:
这是其工作原理的解释:
http://agilerails.wordpress.com/2009/03/11/date_select-time_select-doesnt-work-with-auto_prefix-object/
This is the solution:
Here's the explanation of why it works:
http://agilerails.wordpress.com/2009/03/11/date_select-time_select-doesnt-work-with-auto_prefix-object/
我真心希望这也适用于 date_select :
http://jeffperrin.com/2009/06/04/rails-nested-forms-and-collection_select/
I'd seriously hope that this works for date_select as well:
http://jeffperrin.com/2009/06/04/rails-nested-forms-and-collection_select/
也许这可以与形式宝石一起使用......
Maybe this would work with the formtastic gem...