views:

22

answers:

1

Im making a calendar to display some entries and it needs to select the month and year, i use this in the controller to achive that:

def index
     @month=params[:month].to_i || Time.now.month
     @year=params[:year].to_i || Time.now.year
     @months=[nil, "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
end

When you dont specify a month/year it works fine but when you go to /controller?month=2 it just errors saying "argument out of range" for:

<% offset=Time.parse("1/#{@month}/#{@year}").wday-1 %>

i assume its because its giving the character code instead of 2, but "2".to_i in irb returns 2.

anyone know how to solve this?

A: 

Bizarre fact: nil.to_i returns 0

This is relevant because params[:year] will return nil if it is not specified as an argument (IIRC) and so @year will be set to 0 rather than the expected Time.now.year. This leads to the string your are parsing to be "1/2/0" which is probably causing the error.

This is just my supposition, so test it by outputting the values of @month and @year to check them (or just output the string you are trying to parse).

fd
Yeah i found that, the one i dont use gets set to 0 if the other is defined
Arcath