简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM