gpt4 book ai didi

javascript - rails : remote: true from select_tag

转载 作者:行者123 更新时间:2023-12-02 14:27:10 27 4
gpt4 key购买 nike

我从 select_tag 调用 AJAX 函数,如下所示:

<%= select_tag 'quantity', options_from_collection_for_select(order.options), :quantity, :quantity, order.quantity), onchange: "update_price(#{order.id}, this.value);" %>

这是函数:

<script type='text/javascript'>
function update_price(order_id, quantity) {
$.ajax({
url: "/cart/" + <%= @cart_transaction.id %> + "/update_quantity",
type: "POST",
data: {
"order_id" : order_id,
"quantity" : quantity },
dataType: "html"
});
}
</script>

我的 .js.erb 从未被调用过,我怀疑这是因为我没有在任何地方指定 remote: true ,但因为我没有表格本身我不知道该怎么做。有什么帮助吗?

相关 Controller 代码在这里:

class CartTransactionsController < ApplicationController
load_and_authorize_resource

respond_to :html, :js

before_filter :set_cart_transaction

def update_quantity
@order = @cart_transaction.orders.find(params[:order_id])
@price = current_user.brand.prices
.where(template_id: @order.document.template.id)
.where(quantity: params[:quantity]).first
@order.update_attributes(
price_cents: @price.amount_cents, quantity: params[:quantity]
)
@cart_transaction.save!
respond_to { |format| format.js }
end

private

def set_cart_transaction
@cart_transaction = current_user.cart
end

def cart_transactions_params
params.require(:cart_transaction).permit(
:name, :email, :delivery_address, :comments
)
end
end

更新

这是由于某种原因未调用的 .js.erb:

console.log("update_quantity.js.erb file");

$('#price_cell').html("<%= j render(partial: 'price', locals: { order: @order }) %>");
$('#subtotals').html("<%= j render(partial: 'subtotals', locals: { cart_transaction: @cart_transaction }) %>");

最佳答案

试试这个:

function update_price(order_id, quantity) {
$.ajax({
beforeSend: function(xhr) {
xhr.setRequestHeader('X-CSRF-Token', $('meta[name="csrf-token"]').attr('content'));
},
url: "/cart/" + <%= @cart_transaction.id %> + "/update_quantity",
type: "POST",
data: {
"order_id" : order_id,
"quantity" : quantity }
});
}

关于javascript - rails : remote: true from select_tag,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38122881/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com