繁体   English   中英

Rails无效日期错误

[英]Rails invalid date error

我正在使用 gem在我的应用中设置日历。 除更改月份的箭头外,其他所有工具都工作正常。

ArgumentError in UsersController#show

invalid date
Rails.root: /Users/nelsonkeating/Desktop/ReminDeal

Application Trace | Framework Trace | Full Trace
app/controllers/users_controller.rb:13:in `show'
Request

Parameters:

{"month"=>"2012-06",
 "id"=>"7"}

users_controller.rb

def show
    @user = User.find(params[:id])
    @friends = @user.friends.paginate(page: params[:page])
    @date = params[:month] ? Date.parse(params[:month]) : Date.today
   end

安慰:

Started GET "/users/7?month=2012-06" for 127.0.0.1 at 2012-05-29 21:30:04 -0700
Processing by UsersController#show as HTML
  Parameters: {"month"=>"2012-06", "id"=>"7"}
  User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 7 LIMIT 1
  User Load (0.1ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1  [["id", "7"]]
Completed 500 Internal Server Error in 33ms

ArgumentError (invalid date):
  app/controllers/users_controller.rb:13:in `show'
It is cause due to improper format of the date, the date format is either "%Y-%m-%d" or"%d-%m-%Y" since you have just pass the parameter i.e.
    {"month"=>"2012-06", "id"=>"7"}
you should send the parameter 
    {"month"=>"2012-06-27", "id"=>"7"}
you should send the date also
I had the same problem and solve by adding day in the date parameter.

希望这会帮助你。

是引起错误的Date.parse("2012-06")

如果日期不重要,则可以尝试以下操作: Date.parse("#{params[:month]}-01")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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