ruby on rails - Invalid date with Date.parse -
before user signs require him create challenge.
this challenges_controller:
def create @challenge = challenge.new(challenge_params) if current_user == nil session[:challenge_action] = challenge_params[:action] session[:challenge_deadline] = [params["challenge"]["deadline(3i)"], params["challenge"]["deadline(2i)"], params["challenge"]["deadline(1i)"]].join('/') redirect_to signup_path end end
the parameters passed through:
parameters: {"challenge"=>{"action"=>"run", "deadline"=>"2016-02-29"}, "button"=>""}
the action
, deadline
parameters passed can see above. once new user created new challenge not created:
argumenterror (invalid date): app/controllers/users_controller.rb:33:in `parse' app/controllers/users_controller.rb:33:in `create'
this users_controller:
def create @user = user.new(user_params) if @user.save action = session.delete(:challenge_action) deadline = session.delete(:challenge_deadline) # create if deadline.present? @user.challenges.create( action: action, deadline: date.parse(deadline), #focus of question ) end log_in @user redirect_to root_url end
now if change deadline: date.parse(deadline)
deadline: deadline
challenge
created upon creating new user, deadline
isn't saved reason:
pry(main)> challenge.last id: 1, action: "code", deadline: nil,>
the challenge object won't save invalid deadline value. use text_field
date, use value option.
<%= f.text_field :deadline, value: @challenge.deadline.strftime("%d-%m-%y") %>
there date_field
returns text field of type date. default value generated trying call strftime("%y-%m-%d")
on objects value. able override strftime("%y-%m-%d")
explicitly passing value option (like above).
Comments
Post a Comment