【问题标题】:Why does changing from 'date_select' to 'date_field' effect the use of strftime to display an input time in Rails?为什么从 'date_select' 更改为 'date_field' 会影响使用 strftime 在 Rails 中显示输入时间?
【发布时间】:2014-06-10 19:38:08
【问题描述】:

我有一个用于创建指南的表单。一个重要的组成部分是日期。管理员可以创建新指南,然后将其显示在主页上(如果它在日期范围内)。

为了创建用户友好的 CMS,我将表单从 date_select 更改为 date_field(使用 bootstrap_form_for)。

<%= f.date_select :date_starting %>
<%= f.date_select :date_ending %>

但是,这会在显示指南的时间时导致错误:

undefined method `strftime' for nil:NilClass

提取的源代码:

<h4 class='guide'>From <%= guide.date_starting.strftime("%a %b #{guide.date_starting.day.ordinalize}") %> to <%= guide.date_ending.strftime("%a %b #{guide.date_starting.day.ordinalize}") %></h4>

为什么strftime 的使用不能与date_selectdate_field 表单提交相同?毕竟,我只是在两者中使用相同的信息输入。

【问题讨论】:

    标签: ruby-on-rails ruby forms strftime


    【解决方案1】:

    date_field 只是使用一个字段作为日期/时间。

    date_select 使用三个字段。

    有关如何使用它们的更多信息,请参阅“How to get a Date from date_select or select_date in Rails?”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-10-30
      • 1970-01-01
      • 2022-11-06
      • 1970-01-01
      • 1970-01-01
      • 2018-10-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多