2012-05-30 3 views
0

this 보석을 사용하여 내 앱에서 캘린더를 설정합니다. 그것은 Date.parse("2012-06")가 오류의 원인이되는레일이 잘못된 날짜 오류

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

:

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 

콘솔 : 모든 다음과 같은 오류를 얻기 .. 월을 변경하려면 화살표를 제외하고 잘 작동 . 날짜가 중요하지 않은 경우

, 당신이 시도 할 수 있습니다 : 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. 

희망이 당신을 도울 것입니다.