Rails/Ruby date error
I'm trying to create a new date from form values and开发者_如何学运维 assign it to a variable.
Here's the code I have so far:
eventDate = Date.new(params[:event]["date(1i)"],params[:event]["date(2i)"],params[:event]["date(3i)"])
However, I get the following error:
ArgumentError in EventsController#create
comparison of String with 0 failed
I have no idea what this means.
Any ideas?
Thank you,
Brian
The problem is that Date.new (which actually calls Date.civil) requires that you pass integers, and you are passing strings. Try:
eventDate = Date.new(params[:event]["date(1i)"].to_i,params[:event]["date(2i)"].to_i,params[:event]["date(3i)"].to_i)
You are using String
s. Try this:
eventDate = Date.new(params[:event]["date(1i)"].to_i,params[:event]["date(2i)"].to_i,params[:event]["date(3i)"].to_i)
You are calling the wrong constructor. Date.new doesn't take arguments in the way you're passing.
Date.civil is more what you're looking for which takes (Y,M,D) as arguments. Ruby Date class documentation
精彩评论