i know this question had been asked so many times but despite researching for the past 2 days i still can't get the solution.
currently i implemented bootstrap-switch into my rails apps. after toggling from off to on, the database updated the boolean value correctly.
unfortunately upon visited back to the page with bootstrap-switch, it shows "OFF" and not the saved toggled status "ON".
what should i do so it display the correct status every time it is toggled?
really really appreciated with thanks on any help..
rails view
<%= form_for([:admin, type], remote: true, url: admin_update_ot_path, method: :post) do |f| %>
<div class="field">
<%= f.hidden_field :product_id, :value => @product.id, :id => "pid" %>
<%= f.hidden_field :option_type_id, :value => type.id, :class => "tid" %>
<%= f.check_box :hidden, id: "bootstrapswitch", 'data-type' => type.id %>
</div>
<%= f.submit "update option type" %>
<% end %>
option_type.js.coffee
$(document).on "turbolinks:load", ->
$("[id='bootstrapswitch']").bootstrapSwitch()
option_type.js
$(document).on('turbolinks:load', function() {
$('.edit_option_type input[type=submit]').remove();
$('.edit_option_type input[type=checkbox]').on('switchChange.bootstrapSwitch', function(event, state) {
var product_id = $("#pid").val();
var option_type_id = $(this).data('type');
console.log(option_type_id);
if (state == true) {
$.ajax({
type: "POST",
dataType: "json",
url: "/admin/update_ot",
data: {option_type: {product_id: product_id, option_type_id: option_type_id, hidden: 1}}
});
}
else {
$.ajax({
type: "POST",
dataType: "json",
url: "/admin/update_ot",
data: {option_type: {product_id: product_id, option_type_id: option_type_id, hidden: 0}}
});
}
});
});