【发布时间】:2015-05-16 01:31:24
【问题描述】:
我有一个默认为未选中 (false) 的 rails 礼物选项 check_box_tag。 ajax 调用在检查时正确发布数据(true),但我正在努力弄清楚如何跨页面保存数据。例如,如果我选择礼品选项然后返回购物,当我再次进入购物车页面时,我应该会看到复选框仍处于选中状态。我已经浏览了文档并尝试将 current_cart.gift_option 放在标签内的不同位置,但我不知道如何让复选框显示为已选中,如果它在数据库中为“真”。
show.html.haml
%div.cart-tfoot-gift-option.gift-option-container
%h4.hdr.hdr-quinary Is this order a gift?
%span.form-group-controls
= check_box_tag :gift_option, current_cart.gift_option ? 'true' : 'false', id: 'gift_option'
%label#gift_label.tooltip{:for => 'gift_option'}
Yes, don't display price on packing slip.
Cart.js.coffe
gift_option: ->
$checkbox = $('#gift_option')
$message_container = $('#add-gift-message')
$message_input = $('#gift_message')
$checkbox.on 'change', (event) ->
if $(this).prop('checked')
q.Ajax.message("Adding gift option")
setTimeout () ->
q.Ajax.remove()
, 1000
method_data = { 'gift_option': $checkbox.prop('checked'), 'gift_message': $message_input.val() }
q.Cart.giftAjax(method_data, $message_container.show())
else
q.Ajax.message("Removing gift option")
setTimeout () ->
q.Ajax.remove()
, 1000
method_data = { 'gift_option': $checkbox.prop('checked'), 'gift_message': $message_input.val() }
q.Cart.giftAjax(method_data, $message_container.hide())
$message_input.keyup _.debounce((->
method_data = { 'gift_option': $checkbox.prop('checked'), 'gift_message': $message_input.val() }
q.Cart.giftAjax(method_data)
), 1000)
【问题讨论】:
标签: ajax ruby-on-rails-3 checkbox coffeescript