我知道这个问题被问过很多次但是尽管过去两天的研究仍然无法得到解决方案。
目前我在我的rails应用程序中实现了bootstrap-switch。在从off切换到on之后,数据库正确更新了布尔值。
不幸的是,在使用bootstrap-switch访问回页面时,它显示" OFF"而不是保存的切换状态" ON"。
我应该怎么做才能在每次切换时显示正确的状态?
在任何帮助下真的非常感谢...
<%= 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 %>
$(document).on "turbolinks:load", ->
$("[id='bootstrapswitch']").bootstrapSwitch()
$(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}}
});
}
});
});