This code from my developer below… does it expect something like “1 – January” and try to parse it into something else?
I changed the code on my form so now the value being passed to this controller is just “1” instead of “1 – January”
How can I fix this so I don’t get the “can’t convert nil into string” error?
def get_expiry_month_number(monty_det, expiry_year)
if MONTH_NAMES.include? monty_det
month_number = MONTH_NAMES.index(monty_det)
month_number = MONTH_NUMBERS[month_number]
expiry_year = month_number.to_s + expiry_year[expiry_year.length-3..expiry_year.length-1]
return expiry_year
end
end
In your example you are running into the problem that one of your strings in your calculation for year is nil. Most likely this one,
expiry_year[expiry_year.length-3..expiry_year.length-1]You can try and fix this by calling
to_son the culprit variable, thus converting the nil into''and then concatenated.Like so:
expiry_year = month_number.to_s + expiry_year[expiry_year.length-3..expiry_year.length-1].to_sHowever, this may have other seen consequences and isn’t recommended. A better approach would be to find out why something you expect is a string is turning out to be empty, and perhaps displaying an appropriate error.
Could you explain a little bit more about what the function is supposed to do? And perhaps the value of
expiry_yearandmonty_detwhen the error occurs?putsstatements can be a big help here.