I’m making a calendar to display some entries and it needs to select the month and year, I use this in the controller to achieve 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 it’s because it’s giving the character code instead of 2, but “2”.to_i in irb returns 2.
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).