Tech, Ramblings, and Intellectual Detritus

 
« Back to blog

Use rails date_select without an activerecord model

I actually googled this and found a workable but ugly solution:

 
 ## view code
<%= date_select('range', 'start_date', :order => [:month, :day, :year])%>


## controller code
@start_date = Date.civil(params[:range][:"start_date(1i)"].to_i,params[:range][:"start_date(2i)"].to_i,params[:range][:"start_date(3i)"].to_i) 

I needed to include the hour and minute as well, and didn't want to cram more arguments into the Date.civil call (actually Time.zone.local), so I cleaned up the code a bit.

Hoping to leave the internet a little better for the next guy, I thought I'd post the code.

 
 ## view code
<%= datetime_select('range_start', 'date', :order => [:month, :day, :year, :hour,:minute]) %>


# controller code
@start_date = Time.zone.local(*params[:range_start].sort.map(&:last).map(&:to_i)) 

Comments (3)

Sep 18, 2010
tuckandturn said...
Saved me a lot of time - thanks!
Sep 18, 2010
kevin lochner said...
Happy to hear it
Oct 04, 2010
robinbortlik said...
Thanks a lot. Very nice solution.

Leave a comment...