mysql datetime formatted %m-%d-%Y throws an out of range error in rails form -


i have mysql table field named 'expires' of type datetime

when visit rails edit page table, want display expires date "month/day/year"

in view, edit.html.erb have following

<%= form_for @client, url: {action: "update"} |f| %>        ...    <div class="field">         <%= f.label :expires, "expires"  %>          <%= f.text_field(:expires , :id => 'expires_on')  %>     </div> 

and in controller have

def edit     @client = client.find(params[:id])     @client.expires = @client.expires.strftime("%m/%d/%y") end 

however throws "argument out of range" error if format "%d/%m/%y" don't error

i'm guessing has active record not allowing month first, there way around this?

if absolutely need use formatted field, can add new "virtual" field model:

def expires_formatted   expires.strftime("%m/%d/%y") end  def expires_formatted=(val)   expires = date.strptime(val, "%m/%d/%y") end 

and use expires_formatted instead of expires.

but recommend use jquery ui datepicker editing date field. can read date fields implementations ryan bates episode on railscasts.


Comments