Using select_date gives me back a params[:my_date] with year, month and day attributes. How do get a Date object easily? I’m hoping for something like params[:my_date].to_date.
I’m happy to use date_select instead as well.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Using date_select gives you 3 separate key/value pairs for the day, month, and year respectively. So you can pass them into
Date.newas parameters to create a new Date object.An example date_select returned params for an
Eventmodel:Then to create the new
Dateobject:You may instead decide to wrap this logic in a method:
And then call it as
date = Date.new *flatten_date_array params[:event]. But this is not logic that truly belongs in a controller, so you may decide to move it elsewhere. You could even extend this onto theDateclass, and call it asdate = Date.new_from_hash params[:event].