【问题标题】:date_select tag when upgrading from 2.1.2 to 2.3.8从 2.1.2 升级到 2.3.8 时的 date_select 标记
【发布时间】:2010-07-12 06:09:29
【问题描述】:

我们目前正在升级我们的 rails 版本,我在我的 date_select 标记之一中遇到了一些奇怪的错误。

这是罪魁祸首:

    <%= date_select :consultant_assignment, :start_date, :order => [:day, :month, :year], 
:start_year => 5.years.ago.year, :end_year => 5.years.from_now.year, :use_short_month => true %>

这会抛出如下错误:

NoMethodError in Profiles#show

显示第 46 行出现的 app/views/people/_upcoming_assignments.html.erb:

当你没想到时,你有一个 nil 对象! 您可能期望有一个 Array 的实例。 评估 nil.size 时发生错误

提取的源代码(第 46 行附近):

43:           <label for="consultant_assignment_start_date"><%= _('Start') %>:</label>
44:           <% debugger %>
45:           
46:           <%= date_select :consultant_assignment, :start_date, :order => [:day, :month, :year], :start_year => 5.years.ago.year, :end_year => 5.years.from_now.year, :use_short_month => true %>
47:           
48:           
49:           

我试图追踪这个问题,当我在 :order 数组中输入 :month 选项时,似乎会产生错误。所以当我这样做时 :order => [:day, :year] 页面加载完美。

我已经搜索了确切的问题,但我更像是一个前端设计者,我不知道如何处理这个问题。这是我的代码错误吗?或者它是一种已弃用的方法?

这里是application trace

/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_view/helpers/date_helper.rb:746:in `_unmemoized_month_names'
/opt/local/lib/ruby/gems/1.8/gems/activesupport-2.3.8/lib/active_support/memoizable.rb:72:in `month_names'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_view/helpers/date_helper.rb:781:in `month_name'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_view/helpers/date_helper.rb:708:in `select_month'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_view/helpers/date_helper.rb:705:in `upto'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_view/helpers/date_helper.rb:705:in `select_month'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_view/helpers/date_helper.rb:898:in `send'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_view/helpers/date_helper.rb:898:in `build_selects_from_types'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_view/helpers/date_helper.rb:896:in `each'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_view/helpers/date_helper.rb:896:in `build_selects_from_types'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_view/helpers/date_helper.rb:646:in `select_date'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.3.8/lib/action_view/helpers/date_helper.rb:920:in `to_date_select_tag_without_error_wrapping'

我在谷歌上搜索了未记忆的月份名称,但我找到了 nada。

好的,我已经尝试删除整个 :order 子句,只是为了让这该死的东西正常工作。这次它没有抛出错误,但标签没有出现!我尝试签入 irb,显然我的 date_select 标签没有返回任何选择标签,而只是一堆隐藏标签!

我用默认的 rails 示例尝试了这个:

date_select("post", "written_on")

irb 返回:

=> "\n\n\n"

如您所见,任何地方都没有选择标签。我尝试了 datetime_select ,我在那里看到了一个选择标签。那么 2.3.8 的 date_select 是怎么回事呢?

【问题讨论】:

  • 为我工作。从数据库中检查你的日期是否正确也参考api.rubyonrails.org/classes/ActionView/Helpers/…
  • 我试过看这个,但仍然无济于事。我尝试查看 rails 的新 i18n 东西并制作了 config/locale/en.yml 和 config/initializers/locale.yml,但我仍然遇到同样的错误

标签: ruby-on-rails erb helpers actionpack


【解决方案1】:

使用这些格式

date_select(object_name, method, options = {}, html_options = {})

将 start_year 、 end_year 、 order_field 等放在选项标签中。

我认为这可能会对你有所帮助。

选择已准备好为 Active Record 对象分配多参数。

注意:如果选项中没有包含日期​​但包含月份,则日期将设置为第一天,以确保所有月份选择均有效。

【讨论】:

  • 它仍然显示相同的错误。我认为格式不是问题,因为当我去掉 :month 选项时它可以工作,对吧?
  • 有时格式会产生问题,请通过此链接,它可能对您有所帮助。 api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html
  • 这是 Salil 两天前给我的同一个链接,但我没有找到任何东西:(
【解决方案2】:

这似乎是 i18n 的问题,因为 rails 由于某种原因无法看到声明的月份名称(它应该看到它,因为默认语言是 en)。实际上由于某种原因,它看不到默认语言。无论如何,我们现在的快速解决方法是在 constants.rb 中添加一个 MONTH_NAMES 常量,并将其添加到 date_select 中的选项中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-08
    • 1970-01-01
    • 1970-01-01
    • 2021-05-19
    • 2020-08-07
    • 2014-07-24
    • 1970-01-01
    相关资源
    最近更新 更多