2012-05-30 104 views
0

使用this gem來設置我的應用中的日曆。除了更改月份的箭頭一切正常..收到以下錯誤:Rails無效日期錯誤

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 "https://stackoverflow.com/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' 

回答

1

Date.parse("2012-06")導致錯誤。

如果日期是不顯著,你可以試試這個:Date.parse("#{params[:month]}-01")

+0

完美,謝謝! – js111

2
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. 

希望這會幫助你。