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

By : Tony
Source: Stackoverflow.com
Question!

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 Date.today, { :order => [:month, :day, :year], :start_year => Date.today.year, :end_year => Date.today.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)?

By : Tony


Answers

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 => Date.today, 
    :order => [:month, :day, :year], :start_year => Date.today.year, 
    :end_year => Date.today.year + 3)
By : Sarah Mei


In the case of the image, just set the style to display:none in the <img> tag and then use the other suggestions to remove the CSS attribute Or change it:

$(document).ready(function(){ $("#myImage").css("display","block"); });

This way, the image won't even appear until the document is ready and then the user can click on it. If you need to go the extra mile, do as the other suggested and hide/disable the link also in the tag and use jQuery to show/enable it.

By : hacker


Well, here's my AutoHotkey script:

SetTitleMatchMode RegEx
#IfWinActive, .* - Microsoft Visual Studio
End::Send {End}^e\

Guess I'll use that until someone posts a better answer :)



This video can help you solving your question :)
By: admin