开发者

Issue With .next_month in Ruby

In my local enviroment everything works fine. When I upload to my server, I keep getting an Internal Server Error. I've commented out my code until I found the offending line which is:

 dateObj = dateObj.next_month #Problem Child

Here is the complete code:

def makeCal(dateObj)
    cal = Hash.new
    months = 0
    while months < 12
    #   #pass dateobj to build array
      array = buildArray(dateObj)
    #   #save array to hash with month key
      monthName = Date::MONTHNAMES[dateObj.mon]
      cal[monthName] = array
    #   #create new date object using month and set it to the first
      date = dateObj.month.to_s +  '/' + 1.to_s + '/' + dateObj.year.to_s
      dateObj = Date.strptime(date, "%m/%d/%Y")
      puts dateObj.kind_of? Date
      dateObj = dateObj.next_month #Problem Child
      months = months + 1
    end
    cal

  end

And ruby -v locally:

ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-darwin10.8.0]

and ruby -v remotely:

ruby 1.9.2p290 (2011-07-09 revision 32553) [i686-linux]

Any ideas on how to solve this?

UPDATE:

173.26.190.206 - - [03/Sep/2011 10:40:17] "POST /calendar " 500 30 0.0020

开发者_运维知识库That's from nginx

and this is the stack trace:

NoMethodError - undefined method `next_month' for #<Date: 4911549/2,0,2299161>:
./main.rb:82:in `makeCal'
./main.rb:120:in `POST /calendar'

I inserted the line: puts dateObj.kind_of? Date

and I get all true. So my dateObj is of kind Date


It seems that you lack

require 'active_support'

BTW, if all you need from it is next_month, you can use

date_obj >>= 1

as Date#>> is part of core library.

Edit:

For getting the first of the month, you can use:

Date.new(date_obj.year, date_obj.month)
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜