开发者

Getting Private Method error in Ruby on Rails

开发者 https://www.devze.com 2022-12-19 01:25 出处:网络
I have a controller: class StatsController < ApplicationController require \'time\' def index @started = \"Thu Feb 04 16:12:09 UTC 2010\"

I have a controller:

class StatsController < ApplicationController

  require 'time'

  def index
      @started = "Thu Feb 04 16:12:09 UTC 2010"
      @finished = "Thu Feb 04 16:13:44 UTC 2010"
      @duration_time = stats_duration(@started, @finished)
  end

  private
  def stats_duration(started, finished)
    time_taken = distance_of_time_in_words(Time.parse(started), Time.parse(finished))
    time_taken
  end

end

It takes in a start and end time and calculates the duration between the times.

When I run this I get the following error:

private method `gsub!' called for Thu Feb 04 16:12:09 UTC 2010:Time

Why is this happenin开发者_运维知识库g?


private method gsub! called when using Time.parse usually means that you have called parse with a Time object rather than a String so it sounds like your code is actually trying to parse the time twice.

e.g.

>> t = Time.now
=> Fri Feb 05 13:12:17 +0000 2010
>> Time.parse(t)
NoMethodError: private method `gsub!' called for Fri Feb 05 13:12:17 +0000 2010:Time
        from c:/ruby/lib/ruby/1.8/date/format.rb:965:in `_parse'
        from c:/ruby/lib/ruby/1.8/time.rb:240:in `parse'
        from (irb):6
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号