嵌套模型、表单和 date_select FormHelper 集成

发布于 2024-08-19 09:39:18 字数 829 浏览 2 评论 0原文

我遵循了 Ryan Bates 嵌套模型教程。我的几个嵌套模型都有与之关联的日期。在我的迁移中,它们实际上是“日期”类型。

我尝试过的一些事情和遇到的问题

  1. date_select - 可以处理表单对象前缀,但不能处理嵌套模型属性
  2. select_year - 不适用于表单对象
  3. a通过使用 (Time.now.year - 100)..(Time.now.year) 并覆盖 attr 访问器 start_date 和 end_date 以获取选择中的值来形成日期并将其传回,常规选择填充年份。 仅适用于创建,不适用于更新
  4. 将字段的数据类型更改为字符串,并使用 (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

  1. date_select - can handle the form object prefix, but not nested models attributes
  2. select_year - doesn't work with form object
  3. 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
  4. 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 技术交流群。

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

发布评论

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

评论(3

与往事干杯 2024-08-26 09:39:18

这就是解决方案:

<% new_or_existing = task.new_record? ? 'new' : 'existing' %>
  <% prefix = "project[#{new_or_existing}_task_attributes][]" %>

<% fields_for prefix, task do |t| -%>
   <%= t.date_select(:start_date, :index => task.id || nil) %>
<% end -%>

这是其工作原理的解释:

http://agilerails.wordpress.com/2009/03/11/date_select-time_select-doesnt-work-with-auto_prefix-object/

This is the solution:

<% new_or_existing = task.new_record? ? 'new' : 'existing' %>
  <% prefix = "project[#{new_or_existing}_task_attributes][]" %>

<% fields_for prefix, task do |t| -%>
   <%= t.date_select(:start_date, :index => task.id || nil) %>
<% end -%>

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/

终难遇 2024-08-26 09:39:18

I'd seriously hope that this works for date_select as well:

http://jeffperrin.com/2009/06/04/rails-nested-forms-and-collection_select/

把人绕傻吧 2024-08-26 09:39:18

也许这可以与形式宝石一起使用......

Maybe this would work with the formtastic gem...

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