Skip to content

Instantly share code, notes, and snippets.

@tomciopp
Created May 30, 2012 21:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomciopp/2839011 to your computer and use it in GitHub Desktop.
Save tomciopp/2839011 to your computer and use it in GitHub Desktop.
Adding a datepicker to a nested form
<%= form_for(@load) do |f| %>
<%= f.error_messages %>
<%= f.fields_for :destinations do |builder| %>
<%= render "destination_fields", :f => builder %>
<% end %>
<p><%= link_to_add_fields " Add Destination", f, :destinations %></p>
<% end %>
_destination_fields.html.erb
============================
<fieldset>
<%= f.text_field :arrival_date, :placeholder => "From:", :class => "datepicker" %>
</fieldset>
jQuery ->
$('form').on 'click', '.remove_fields', (event) ->
$(this).prev('input[type=hidden]').val('1')
$(this).closest('fieldset').hide()
event.preventDefault()
$('form').on 'click', '.add_fields', (event) ->
time = new Date().getTime()
regexp = new RegExp($(this).data('id'), 'g')
$(this).before($(this).data('fields').replace(regexp, time))
event.preventDefault()
$(function() {
$(".datepicker").datepicker({ dateFormat: 'yy-mm-dd', autoSize:true });
});
$(function() {
$('.add_fields').on('click', function(){
$(this).datepicker({ dateFormat: 'yy-mm-dd', autoSize:true });
});
});
I am trying to add a calendar date picker to a nested form and my code works for the fields I have generated, but when I go to add another field to the DOM, the "datepicker" class is not added to the new field. I have a feeling that I just need to add a correct callback function in the datepicker js file, however I am unsure of how that would look.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment