Datetimepicker not showing date with cocoon's nested form - Rails











up vote
2
down vote

favorite












I have a nested form _form.html.erb within my homepage and I added another nested form _invsession_fields.html.erb that is appended to the first form using cocoon gem. _invsession_fields.html.erb has only two fields which are datetime type. I am trying to get the datetimepicker to work dynamically when the user add time slots.



_form.html.erb :



    <div id="theform" class="col-md-12 homepage_box_top_dashboard">
<h3 class="section_title"> Schedule event with IR </h3>
<%= simple_form_for invitation, id: "invitation_form", :remote => true do |f| %>
<div class="col-md-12">
<%= f.error_notification %>
<%= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present? %>
<%= f.collection_select(:guest_id, User.all, :id, :full_name, {}, class: "selectpicker", title: "Choose recipient", multiple: true) %>
</div>
<div class="col-md-12">
<%= f.collection_select(:type_of_event, @type_evenement, :id, :type_name, {:include_blank => true}, class: "selectpicker", title: "Type", multiple: false) %>
</div>
<div class="col-md-12">
<%= f.collection_select(:event_id, @events_list, :id, :title, {:include_blank => true}, class: "selectpicker", title: "Related event (Optional)", multiple: false) %>
<%= f.input :title, placeholder: "Title", label: false %>
<%= f.text_area :memo, placeholder: "Start writing", class: "form form-control", rows: 10 %>
<div id="invsessions">
<%= f.simple_fields_for :invsessions do |builder| %>
<%= render 'invitations/invsession_fields', f: builder %>
<% end %>
</div>
<div class="form-actions">
<br>
<%= f.button :submit, class: "btn btn-primary" %>
<br>
<%= link_to_add_association 'Add time slots', f, :invsessions, class: 'btn btn-primary', partial: 'invitations/invsession_fields' %>
</div>
</div>
<% end %>
</div>
<script type="text/javascript">
$(document).ready(function () {
$('#invitation_type_of_event').select2({
placeholder: "Select event type",
allowClear: true
});
});
$(document).ready(function () {
$('#invitation_event_id').select2({
placeholder: "Related event (Optional)",
allowClear: true

});
});
$(document).on('ready page:change', function() {
$('.invsessions').on('cocoon:after-insert', function() {
$('#datetimepicker').datetimepicker();
})
})
</script>


_invsession_fields.html.erb :



<div class="nested-fields">
<tr>
<%= f.input_field :_destroy, as: :hidden %>
<%= link_to_remove_association "Remove slot", f, class: 'btn btn-primary btn-xs' %>
</tr>
<tr>
<div class='input-group date' id='datetimepicker'>
<%= f.text_field :start, :class => "form-control" %>
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span><br>
</div>
</tr>
<tr>
<div class='input-group date' id='datetimepicker'>
<%= f.text_field :end, :class => "form-control" %>
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</tr>
</div>


Does anybody knows how I could automatically regenerate the datetimepicker formating when the nested form is appended please ?



I tried multiple solutions that i found on search engines, but none of them work. I know i should be adding a few lines within my SCRIPT, but not sure which one, and where. Can someone help pls ?



I added :



$(document).on('ready page:change', function() {
$('#invsessions').on('cocoon:after-insert', function() {
$('.datetimepicker').datetimepicker();
})
})


Within the <SCRIPT> in the _form.html.erb but no luck










share|improve this question
























  • in your code you have $('.datetimepicker').datetimepicker(); (refer to class) but in your html you do not have class datetimepicker, you have elements with datetimepicker id, this is not recommended because id must be unique. Change in your html id=datetimepicker to class='datetimepicker ...'
    – inye
    Nov 5 at 12:41















up vote
2
down vote

favorite












I have a nested form _form.html.erb within my homepage and I added another nested form _invsession_fields.html.erb that is appended to the first form using cocoon gem. _invsession_fields.html.erb has only two fields which are datetime type. I am trying to get the datetimepicker to work dynamically when the user add time slots.



_form.html.erb :



    <div id="theform" class="col-md-12 homepage_box_top_dashboard">
<h3 class="section_title"> Schedule event with IR </h3>
<%= simple_form_for invitation, id: "invitation_form", :remote => true do |f| %>
<div class="col-md-12">
<%= f.error_notification %>
<%= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present? %>
<%= f.collection_select(:guest_id, User.all, :id, :full_name, {}, class: "selectpicker", title: "Choose recipient", multiple: true) %>
</div>
<div class="col-md-12">
<%= f.collection_select(:type_of_event, @type_evenement, :id, :type_name, {:include_blank => true}, class: "selectpicker", title: "Type", multiple: false) %>
</div>
<div class="col-md-12">
<%= f.collection_select(:event_id, @events_list, :id, :title, {:include_blank => true}, class: "selectpicker", title: "Related event (Optional)", multiple: false) %>
<%= f.input :title, placeholder: "Title", label: false %>
<%= f.text_area :memo, placeholder: "Start writing", class: "form form-control", rows: 10 %>
<div id="invsessions">
<%= f.simple_fields_for :invsessions do |builder| %>
<%= render 'invitations/invsession_fields', f: builder %>
<% end %>
</div>
<div class="form-actions">
<br>
<%= f.button :submit, class: "btn btn-primary" %>
<br>
<%= link_to_add_association 'Add time slots', f, :invsessions, class: 'btn btn-primary', partial: 'invitations/invsession_fields' %>
</div>
</div>
<% end %>
</div>
<script type="text/javascript">
$(document).ready(function () {
$('#invitation_type_of_event').select2({
placeholder: "Select event type",
allowClear: true
});
});
$(document).ready(function () {
$('#invitation_event_id').select2({
placeholder: "Related event (Optional)",
allowClear: true

});
});
$(document).on('ready page:change', function() {
$('.invsessions').on('cocoon:after-insert', function() {
$('#datetimepicker').datetimepicker();
})
})
</script>


_invsession_fields.html.erb :



<div class="nested-fields">
<tr>
<%= f.input_field :_destroy, as: :hidden %>
<%= link_to_remove_association "Remove slot", f, class: 'btn btn-primary btn-xs' %>
</tr>
<tr>
<div class='input-group date' id='datetimepicker'>
<%= f.text_field :start, :class => "form-control" %>
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span><br>
</div>
</tr>
<tr>
<div class='input-group date' id='datetimepicker'>
<%= f.text_field :end, :class => "form-control" %>
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</tr>
</div>


Does anybody knows how I could automatically regenerate the datetimepicker formating when the nested form is appended please ?



I tried multiple solutions that i found on search engines, but none of them work. I know i should be adding a few lines within my SCRIPT, but not sure which one, and where. Can someone help pls ?



I added :



$(document).on('ready page:change', function() {
$('#invsessions').on('cocoon:after-insert', function() {
$('.datetimepicker').datetimepicker();
})
})


Within the <SCRIPT> in the _form.html.erb but no luck










share|improve this question
























  • in your code you have $('.datetimepicker').datetimepicker(); (refer to class) but in your html you do not have class datetimepicker, you have elements with datetimepicker id, this is not recommended because id must be unique. Change in your html id=datetimepicker to class='datetimepicker ...'
    – inye
    Nov 5 at 12:41













up vote
2
down vote

favorite









up vote
2
down vote

favorite











I have a nested form _form.html.erb within my homepage and I added another nested form _invsession_fields.html.erb that is appended to the first form using cocoon gem. _invsession_fields.html.erb has only two fields which are datetime type. I am trying to get the datetimepicker to work dynamically when the user add time slots.



_form.html.erb :



    <div id="theform" class="col-md-12 homepage_box_top_dashboard">
<h3 class="section_title"> Schedule event with IR </h3>
<%= simple_form_for invitation, id: "invitation_form", :remote => true do |f| %>
<div class="col-md-12">
<%= f.error_notification %>
<%= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present? %>
<%= f.collection_select(:guest_id, User.all, :id, :full_name, {}, class: "selectpicker", title: "Choose recipient", multiple: true) %>
</div>
<div class="col-md-12">
<%= f.collection_select(:type_of_event, @type_evenement, :id, :type_name, {:include_blank => true}, class: "selectpicker", title: "Type", multiple: false) %>
</div>
<div class="col-md-12">
<%= f.collection_select(:event_id, @events_list, :id, :title, {:include_blank => true}, class: "selectpicker", title: "Related event (Optional)", multiple: false) %>
<%= f.input :title, placeholder: "Title", label: false %>
<%= f.text_area :memo, placeholder: "Start writing", class: "form form-control", rows: 10 %>
<div id="invsessions">
<%= f.simple_fields_for :invsessions do |builder| %>
<%= render 'invitations/invsession_fields', f: builder %>
<% end %>
</div>
<div class="form-actions">
<br>
<%= f.button :submit, class: "btn btn-primary" %>
<br>
<%= link_to_add_association 'Add time slots', f, :invsessions, class: 'btn btn-primary', partial: 'invitations/invsession_fields' %>
</div>
</div>
<% end %>
</div>
<script type="text/javascript">
$(document).ready(function () {
$('#invitation_type_of_event').select2({
placeholder: "Select event type",
allowClear: true
});
});
$(document).ready(function () {
$('#invitation_event_id').select2({
placeholder: "Related event (Optional)",
allowClear: true

});
});
$(document).on('ready page:change', function() {
$('.invsessions').on('cocoon:after-insert', function() {
$('#datetimepicker').datetimepicker();
})
})
</script>


_invsession_fields.html.erb :



<div class="nested-fields">
<tr>
<%= f.input_field :_destroy, as: :hidden %>
<%= link_to_remove_association "Remove slot", f, class: 'btn btn-primary btn-xs' %>
</tr>
<tr>
<div class='input-group date' id='datetimepicker'>
<%= f.text_field :start, :class => "form-control" %>
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span><br>
</div>
</tr>
<tr>
<div class='input-group date' id='datetimepicker'>
<%= f.text_field :end, :class => "form-control" %>
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</tr>
</div>


Does anybody knows how I could automatically regenerate the datetimepicker formating when the nested form is appended please ?



I tried multiple solutions that i found on search engines, but none of them work. I know i should be adding a few lines within my SCRIPT, but not sure which one, and where. Can someone help pls ?



I added :



$(document).on('ready page:change', function() {
$('#invsessions').on('cocoon:after-insert', function() {
$('.datetimepicker').datetimepicker();
})
})


Within the <SCRIPT> in the _form.html.erb but no luck










share|improve this question















I have a nested form _form.html.erb within my homepage and I added another nested form _invsession_fields.html.erb that is appended to the first form using cocoon gem. _invsession_fields.html.erb has only two fields which are datetime type. I am trying to get the datetimepicker to work dynamically when the user add time slots.



_form.html.erb :



    <div id="theform" class="col-md-12 homepage_box_top_dashboard">
<h3 class="section_title"> Schedule event with IR </h3>
<%= simple_form_for invitation, id: "invitation_form", :remote => true do |f| %>
<div class="col-md-12">
<%= f.error_notification %>
<%= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present? %>
<%= f.collection_select(:guest_id, User.all, :id, :full_name, {}, class: "selectpicker", title: "Choose recipient", multiple: true) %>
</div>
<div class="col-md-12">
<%= f.collection_select(:type_of_event, @type_evenement, :id, :type_name, {:include_blank => true}, class: "selectpicker", title: "Type", multiple: false) %>
</div>
<div class="col-md-12">
<%= f.collection_select(:event_id, @events_list, :id, :title, {:include_blank => true}, class: "selectpicker", title: "Related event (Optional)", multiple: false) %>
<%= f.input :title, placeholder: "Title", label: false %>
<%= f.text_area :memo, placeholder: "Start writing", class: "form form-control", rows: 10 %>
<div id="invsessions">
<%= f.simple_fields_for :invsessions do |builder| %>
<%= render 'invitations/invsession_fields', f: builder %>
<% end %>
</div>
<div class="form-actions">
<br>
<%= f.button :submit, class: "btn btn-primary" %>
<br>
<%= link_to_add_association 'Add time slots', f, :invsessions, class: 'btn btn-primary', partial: 'invitations/invsession_fields' %>
</div>
</div>
<% end %>
</div>
<script type="text/javascript">
$(document).ready(function () {
$('#invitation_type_of_event').select2({
placeholder: "Select event type",
allowClear: true
});
});
$(document).ready(function () {
$('#invitation_event_id').select2({
placeholder: "Related event (Optional)",
allowClear: true

});
});
$(document).on('ready page:change', function() {
$('.invsessions').on('cocoon:after-insert', function() {
$('#datetimepicker').datetimepicker();
})
})
</script>


_invsession_fields.html.erb :



<div class="nested-fields">
<tr>
<%= f.input_field :_destroy, as: :hidden %>
<%= link_to_remove_association "Remove slot", f, class: 'btn btn-primary btn-xs' %>
</tr>
<tr>
<div class='input-group date' id='datetimepicker'>
<%= f.text_field :start, :class => "form-control" %>
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span><br>
</div>
</tr>
<tr>
<div class='input-group date' id='datetimepicker'>
<%= f.text_field :end, :class => "form-control" %>
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
</tr>
</div>


Does anybody knows how I could automatically regenerate the datetimepicker formating when the nested form is appended please ?



I tried multiple solutions that i found on search engines, but none of them work. I know i should be adding a few lines within my SCRIPT, but not sure which one, and where. Can someone help pls ?



I added :



$(document).on('ready page:change', function() {
$('#invsessions').on('cocoon:after-insert', function() {
$('.datetimepicker').datetimepicker();
})
})


Within the <SCRIPT> in the _form.html.erb but no luck







ruby-on-rails






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 5 at 2:23

























asked Nov 5 at 1:59









Etienne

926




926












  • in your code you have $('.datetimepicker').datetimepicker(); (refer to class) but in your html you do not have class datetimepicker, you have elements with datetimepicker id, this is not recommended because id must be unique. Change in your html id=datetimepicker to class='datetimepicker ...'
    – inye
    Nov 5 at 12:41


















  • in your code you have $('.datetimepicker').datetimepicker(); (refer to class) but in your html you do not have class datetimepicker, you have elements with datetimepicker id, this is not recommended because id must be unique. Change in your html id=datetimepicker to class='datetimepicker ...'
    – inye
    Nov 5 at 12:41
















in your code you have $('.datetimepicker').datetimepicker(); (refer to class) but in your html you do not have class datetimepicker, you have elements with datetimepicker id, this is not recommended because id must be unique. Change in your html id=datetimepicker to class='datetimepicker ...'
– inye
Nov 5 at 12:41




in your code you have $('.datetimepicker').datetimepicker(); (refer to class) but in your html you do not have class datetimepicker, you have elements with datetimepicker id, this is not recommended because id must be unique. Change in your html id=datetimepicker to class='datetimepicker ...'
– inye
Nov 5 at 12:41

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53147368%2fdatetimepicker-not-showing-date-with-cocoons-nested-form-rails%23new-answer', 'question_page');
}
);

Post as a guest





































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53147368%2fdatetimepicker-not-showing-date-with-cocoons-nested-form-rails%23new-answer', 'question_page');
}
);

Post as a guest




















































































這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()