1

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}}
            });
    }

});
});
ckeat9
  • 162
  • 1
  • 14
  • Could you add some necessary / relevant code snippet so someone can understand your problem and proffer a good solution – Chidi Ekuma Mar 11 '17 at 17:22
  • updated as per request.. after opening new page, even if the "ON" state was toggled and saved in database, reopening the page in new tab still display the "OFF" state. – ckeat9 Mar 11 '17 at 17:27
  • I wanted to ask how you get in the field f.checkbox the value from the database (:admin.hidden) ? Thanks a lot – Fabrizio Bertoglio Mar 11 '17 at 19:06

0 Answers0