Rails 3.1 text_field form helper and jQuery datepicker date format

Posted by DanyW on Stack Overflow See other posts from Stack Overflow or by DanyW
Published on 2011-11-12T08:11:37Z Indexed on 2011/11/12 9:51 UTC
Read the original article Hit count: 341

I have a form field in my Rails view like this:

<%= f.text_field :date, :class => "datepicker" %>

A javascript function converts all input fields of that class to a jQueryUI datepicker.

$(".datepicker").datepicker({
                dateFormat : "dd MM yy",
                buttonImageOnly : true,
                buttonImage : "<%= asset_path('iconDatePicker.gif') %>",
                showOn : "both",
                changeMonth : true,
                changeYear : true,
                yearRange : "c-20:c+5"
            })

So far so good. I can edit the record and it persists the date correctly all the way to the DB. My problem is when I want to edit the record again. When the form is pre-populated from the record it displays in 'yyyy-mm-dd' format. The javascript only formats dates which are selected in the datepicker control. Is there any way I can format the date retrieved from the database? At which stage can I do this?

Thanks, Dany.

Some more details following the discussion below, my form is spread across two files, a view and a partial. Here's the view:

<%= form_tag("/shows/update_individual", :method => "put") do %>
    <% for show in @shows %>
        <%= fields_for "shows[]", show do |f| %>
            <%= render "fields", :f => f %>
        <% end %>
    <% end %>
    <%= submit_tag "Submit"%>
<% end %>

And here's the _fields partial view:

<p>
    <%= f.label :name %>
    <%= f.text_field :name %>
</p>

<p>
    <%= f.date %>
    <%= f.label :date %>
    <%= f.text_field :date, :class => "datepicker" %>
</p>

Controller code:

def edit_individual
    @shows = Show.find(params[:show_ids])
  end

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about jquery-ui