ArgumentError (wrong date) in Ruby On Rails

I get this ArgumentError (wrong date) and cannot figure out what causes it. This method is called from javascript.

  def loadDay
     @first_day_of_week = Date.strptime("{ params[:year].to_s, :params[:month].to_s, params[:day].to_s }", "{ %Y, %m, %d }")

     ...
  end

The log is as follows:

  Started GET "/planner/loadDay?day=7&month=5&year=2011" for 127.0.0.1 at 2011-05-15 10:19:43 +0200
  Processing by PlannerController#loadDay as */*
  Parameters: {"day"=>"7", "month"=>"5", "year"=>"2011"}
  Completed   in 1ms

Please point me in the right direction.

+1
source share
1 answer

You are missing a few #{}for string interpolation in the first argument Date.strptime. I think you want to say this:

Date.strptime("{ #{params[:year]}, #{params[:month]}, #{params[:day]} }", "{ %Y, %m, %d }")

Your call just passed this literal string to strptime:

"{ params[:year].to_s, :params[:month].to_s, params[:day].to_s }"

and without the values ​​from paramsreplaced by in, I strptimedidn’t know what you said, was upset and raised an exception ArgumentErrorto indicate "invalid date".

to_s #{}, #{} , params (, , params , Rails).

+2

All Articles