Hi Colin, Sayuj and Michael,

This is a proprietary, in-house app in Rails 3.1rc4 for the U.S market and they 
hate date_select ("Date is a little hard to fill out. It is time-consuming to 
click on "January" and scroll to "June", etc. Any chance I could type in 
6/3/11?").

>If you want to allow the user to enter dates like this you will have
>to parse it yourself to make sure the month and day are as you want
>them.  If the site is for an international audience I suggest you use
>separate fields for the three components as otherwise you will have no
>end of trouble as most of the world uses day/month/year.  I don't
>understand what you mean by "it happens before the column is available
>in the controller".  The field will be passed as a string from the
>form to the controller and should appear in params exactly as entered
>by the user.  Note also that, since you have specified a date field in
>the database, what is stored there is not "2011-07-11" or any other
>string, but is the date itself.

Colin, you are right that params[:start_date] is exactly as entered in the 
form. 

What I ended up doing was handling it in the controller with:
[JobsController]
  def create
    @job = Job.new(params[:job])
    @job.start_date = parse_free_date(params[:job][:start_date])
    @job.due_date = parse_free_date(params[:job][:due_date])

and
 private
 def parse_free_date(date_str)  
    case date_str
    when /\A(\d|\d\d)\/(\d|\d\d)\/(\d\d)\z/ # "6/3/11" or "06/03/11"
      parts = date_str.split('/', 3)  
      "20#{parts[2]}-#{parts[0]}-#{parts[1]}"
    when /\A\d{4}-\d{2}-\d{2}\z/ # "2011-07-11"
      date_str
    when /\A(\d|\d\d)\/(\d|\d\d)\/(\d\d\d\d)\z/ # "6/3/2011" or "06/03/2011"
      parts = date_str.split('/', 3)  
      "#{parts[2]}-#{parts[0]}-#{parts[1]}"
    when /\A(\d|\d\d)-(\d|\d\d)-(\d\d\d\d)\z/ # "6-3-2011" or "06-03-2011"
      parts = date_str.split('/', 3)  
      "#{parts[2]}-#{parts[0]}-#{parts[1]}"
    end
  end

I'll tighten up the regexes later; they are good enough for a prototype.

Thanks for all your help!

**Leigh



-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to