Ruby on rails - NoMethodError (未定义的方法'year' for "%m/%d/%Y %H:%M" :String)



我在rails应用程序上进行了捆绑包更新,现在我得到了以下错误

NoMethodError (undefined method `year' for "%m/%d/%Y %H:%M":String):
  app/controllers/onload_wips_controller.rb:44:in `parse_times_to_local_format'

以下是parse_times_to_local_format方法

  def parse_times_to_local_format
    shifts = Delay.get_shifts(@default[:scope])
    if (@default[:start_time].size < 11)
      @default[:start_time] = @default[:start_time]+' '+shifts.first
    end
    if (@default[:end_time].size < 11)
      @default[:end_time] = @default[:end_time]+' '+shifts.first
    end
    @default[:start_time] = Time.zone.parse(@default[:start_time],"%m/%d/%Y %H:%M")
    @default[:end_time] = Time.zone.parse(@default[:end_time],"%m/%d/%Y %H:%M")

  end

不知道该做什么,下面是我的捆绑包显示:

Gems included by the bundle:
  * ZenTest (4.9.5)
  * accept_values_for (0.7.0 46dd22e)
  * actionmailer (3.2.17)
  * actionpack (3.2.17)
  * activemodel (3.2.17)
  * activerecord (3.2.17)
  * activerecord-sqlserver-adapter (3.2.12)
  * activeresource (3.2.17)
  * activesupport (3.2.17)
  * acts_as_audited (2.0.0)
  * acts_as_xlsx (1.0.6)
  * american_date (1.1.0)
  * archive-tar-minitar (0.5.2)
  * arel (3.0.3)
  * autotest-fsevent (0.2.9)
  * autotest-growl (0.2.16)
  * autotest-rails (4.1.2)
  * axlsx (2.0.1)
  * axlsx_rails (0.1.5)
  * bcrypt (3.1.7)
  * bcrypt-ruby (3.1.5)
  * bootstrap-sass (2.1.1.0)
  * builder (3.0.4)
  * bundler (1.3.5)
  * cancan (1.6.10)
  * capistrano (2.14.2)
  * capistrano-ext (1.2.1)
  * chronic (0.6.7)
  * coffee-rails (3.2.2)
  * coffee-script (2.2.0)
  * coffee-script-source (1.7.0)
  * columnize (0.3.6)
  * cssbuttongenerator-css-rails (1.0.5)
  * daemon_controller (1.2.0)
  * database_cleaner (1.2.0)
  * devise (1.5.3)
  * diff-lcs (1.2.5)
  * enableplaceholder-jquery-rails (1.2.2.1)
  * erubis (2.7.0)
  * execjs (2.0.2)
  * fail-fast (1.1.0)
  * fastthread (1.0.7)
  * ffi (1.9.3)
  * font-awesome-rails (4.0.3.1)
  * github-css-rails (1.1.3)
  * haml (4.0.5)
  * highline (1.6.21)
  * hike (1.2.3)
  * htmlentities (4.3.1)
  * i18n (0.6.9)
  * journey (1.0.4)
  * jqgrid-jquery-rails (4.2.0.0)
  * jquery-rails (1.0.19)
  * jquery-ui-rails (4.0.5)
  * json (1.8.1)
  * linecache19 (0.5.12)
  * logger-jquery-rails (1.2.0.3)
  * machinist (2.0)
  * mail (2.5.4)
  * mime-types (1.25.1)
  * mini_portile (0.5.3)
  * mocha (0.9.12)
  * multi_json (1.9.2)
  * mysql2 (0.3.15)
  * net-scp (1.2.0)
  * net-sftp (2.1.2)
  * net-ssh (2.8.0)
  * net-ssh-gateway (1.2.0)
  * nokogiri (1.6.1)
  * orm_adapter (0.0.7)
  * passenger (3.0.21)
  * polyglot (0.3.4)
  * qtip2-jquery-rails (2.1.1.5)
  * rack (1.4.5)
  * rack-cache (1.2)
  * rack-ssl (1.3.4)
  * rack-test (0.6.2)
  * rails (3.2.17)
  * railties (3.2.17)
  * rake (10.2.2)
  * rdoc (3.12.2)
  * redcarpet (3.1.1)
  * rinruby (2.0.3)
  * rspec (2.14.1)
  * rspec-core (2.14.8)
  * rspec-expectations (2.14.5)
  * rspec-mocks (2.14.6)
  * rspec-rails (2.14.2)
  * ruby-debug-base19 (0.11.25)
  * ruby-debug19 (0.11.6)
  * ruby-prof (0.11.0 ffae61a)
  * ruby_core_source (0.1.5)
  * ruby_parser (2.0.6)
  * rubyzip (1.0.0)
  * rvm-capistrano (1.2.7)
  * sass (3.1.12)
  * sass-rails (3.2.6)
  * sexp_processor (3.2.0)
  * shoulda-matchers (2.6.0)
  * simple_form (2.1.1)
  * sprockets (2.2.2)
  * sys-uname (0.9.2)
  * tablecloth-rails (1.0.101)
  * the1kbgrid-css-rails (1.1.2)
  * thor (0.19.1)
  * tilt (1.4.1)
  * timecop (0.3.5)
  * timeliness (0.3.7)
  * tiny_tds (0.6.1)
  * treetop (1.4.15)
  * tzinfo (0.3.39)
  * uglifier (1.2.7)
  * underscore-rails (1.6.0)
  * validates_timeliness (3.0.14)
  * warden (1.2.3)
  * whenever (0.7.3)
  * will_paginate (3.0.5)
  * yui-compressor (0.12.0)

更新

这似乎是一个bug,在rails控制台上执行:

1.9.3-p392 :001 > date = Time.zone.parse("02/02/14 06:00","%m/%d/%Y %H:%M")

给了我同样的错误:

NoMethodError: undefined method `year' for "%m/%d/%Y %H:%M":String
    from /home/warch/.rvm/gems/ruby-1.9.3-p392/gems/activesupport-3.2.17/lib/active_support/values/time_zone.rb:275:in `parse'

我想您想使用方法.strptime:

> DateTime.strptime("01/01/2001 19:00","%m/%d/%Y %H:%M")
=> Mon, 01 Jan 2001 19:00:00 +0000

在您的情况下:

@default[:start_time] = DateTime.strptime(@default[:start_time], "%m/%d/%Y %H:%M")
@default[:end_time]   = DateTime.strptime(@default[:end_time], "%m/%d/%Y %H:%M")

文件:http://apidock.com/ruby/DateTime/strptime/class

相关内容

  • 没有找到相关文章

最新更新