【问题标题】:Undefined method "getlocal" when displaying created_at?显示 created_at 时未定义方法“getlocal”?
【发布时间】:2012-06-29 20:56:16
【问题描述】:

我使用 rails 3.2.6、ruby 1.9.3 和 mongoid gem 3.0。

我想显示数据库条目的 created_at 字段,但出现以下错误:

undefined method `getlocal' for "Wed, 25 Apr 2012 15:04:37 -0400":String

这是rails代码:

<dt>Erstellt am:</dt><dd><%= @app.created_at %></dd>

任何建议有什么问题?有错误修正吗?在我看来应该有效吗?

提前致谢!

【问题讨论】:

  • 我想我找到了你的答案here。希望对您有所帮助...
  • 之前看到过,但我不知道如何将它用于 created_at 变量。有什么建议吗?
  • 尝试将其转换为字符串@app.created_at.to_s
  • 也看看here
  • @Tronic 在控制台中,@app.created_at.class 返回什么?它是否返回这个=&gt; ActiveSupport::TimeWithZone,如果没有,为什么它被存储为其他东西?

标签: ruby-on-rails mongoid


【解决方案1】:

getlocalTime 类的方法,因此可能是混合对象类型的问题。系统期望 @app.created_at 是 Time 的实例,不是 DateTime。确保created_at 的字段类型为 DateTime,并且在创建/更新此字段时,请确保您放入的对象也是 DateTime 对象。

【讨论】:

  • 我用mongoid,它会自动设置created_at时间戳,所以不知道...
  • 如何确定?我试过field :createdAt, type: DateTime,但它仍然崩溃
【解决方案2】:

尝试将以下内容添加到您的模型中

include Mongoid::Timestamps

http://mongoid.org/en/mongoid/docs/extras.html#timestamps

【讨论】:

    【解决方案3】:

    如果你使用 mongoid,而不是
    @app.created_at
    试试
    @app[:created_at]
    这对我有用

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多