Wednesday, April 20, 2011

How do I handle date objects in ruby on rails forms?

I am using a rails helper that creates 3 select lists for the purpose of letting a user select a date. If I assign the helper a name, it sets all 3 select lists to have that name, and then only the last one goes through (it overwrites the previous). What I need to know is: What is the best way to take a set of 3 select lists (day, month, year), and combine them to create a date object and save it to my database?

Here is my date field:

<%= select_date, { :order => [:month, :day, :year], :start_year =>, :end_year => + 3 } %>

and here is the parameters passed to my controller (spat out from my dev log)

Parameters: {"date"=>{"month"=>"4", "day"=>"9", "year"=>"2009"}, 
"action"=>"create", "controller"=>"shows", 
"show"=>{"headline"=>"for example, \"Parking lot is next to the bookstore on Main Street\"", "venue_id"=>{"venue_id"=>""}, "cents"=>"00", "dollars"=>"00"}, 
"user_id"=>"3", "load_in"=>"-2", "show_time"=>"-2", "query"=>"Type venue name & location here...", "doors"=>"-2"}

As you can see, the date is passed. Ideally, I would like the date passed as an acceptable date object to my controller. Furthermore, I would like it passed in the "show" hash as it is show data. Is this possible with some simple rails modifications? Or do I have to write some JS (...a bit dirty for this solution in my opinion)?

From stackoverflow
  • I think you want date_select instead of select_date, since you want it associated with a particular field in your show object.

      date_select("show", "date", :default =>, 
        :order => [:month, :day, :year], :start_year =>, 
        :end_year => + 3)


Post a Comment