Rails 4 AJAX不会更新会话的变量

时间:2014-07-12 09:00:29

标签: jquery ajax session ruby-on-rails-4

当我通过AJAX在Rails 4中尝试更新会话时,我遇到了问题。我有一些coffescript:

ready = ->
$('#_usa-shipping_free').change (event) ->
  if $('#_usa-shipping_free').prop('checked')
    select_wrapper = $('#shipping-wrapper')

    select_wrapper.empty

    url = "/shipping_cart?shipping_type=free&remote=true"

    select_wrapper.load(url)
    updateTotal()
  return

$('#_usa-shipping_priority').change (event) ->
  if $('#_usa-shipping_priority').prop('checked')
    select_wrapper = $('#shipping-wrapper')

    select_wrapper.empty

    url = "/shipping_cart?shipping_type=priority&remote=true"

    select_wrapper.load(url)
    updateTotal()
  return

updateTotal = ->
  $.ajax(
    url: "http://localhost:3000/order/get_total",
    contentType: 'text/plain'
  ).success (data) ->
    $("#order-total").html("Total $" + data.total)
    return
  .fail ->

return

$(document).ready(ready)
$(document).on('page:load', ready)

控制器动作:

def shipping_cart(shipping_type = params[:shipping_type])

  if shipping_type == 'international'
    session[:shipping_cost] = Order::INTERNATIONAL_SHIPPING_COST
  elsif shipping_type == 'priority'
    session[:shipping_cost] = Order::PRIORITY_SHIPPING_COST
  else
    session[:shipping_cost] = Order::FREE_SHIPPING_COST
  end
  if params[:remote]
    render partial: 'shipping_cart'
    return
  end
end

此代码必须根据包含运输选项的单选按钮更新总订单数量的字段。相反,它无论如何都不起作用。如果我删除了updateTotal()的调用,那么下一步就是它的工作:打开页面 - >选择广播 - >刷新页面 - >总计被重述。但我需要在AJAX中使用它。我将会话用于TOTAL和SHIPPING_COST。

这是我的GET_TOTAL行动:

  def get_total
    total = 0.to_f
    books = Book.all
    books.each do |book|
      if session[:cart].include? book.id
        total = total + book.price.to_f
      end
    end
    if session[:discount_code] != nil && session[:discount_code].blank? == false
      discount_code = DiscountCode.find_by_code(session[:discount_code])
      unless discount_code.nil?
        unless discount_code.fixed_discount.blank?
          total = total - discount_code.fixed_discount
        else
          total = total - (total / 100 * discount_code.discount_percents  )
        end
      end
    end
    total = total + session[:shipping_cost].to_f
    total = total.round(2)
    session[:total] = total
    output = {'total' => "#{total}"}.to_json
    respond_to do |format|
      format.json { render json: output, status: 200 }
      format.html { total }
    end
 end

在每次操作之前,除了SUBMIT(在其中我尝试使用所有这些功能)之前,我将SHIPPING_COST设置为0。

当我尝试使用puts session [:shipping_cost]将shipping_cost显示到控制台时,在shipping_cart中我得到正确的值,但在get_total - 0。

有时候功能有效,但有几次有效,我点击FREE_SHIPPING就行了,但我的TOTAL加上PRIORITY_SHIPPING。

这是我的提交行动,也许有帮助:

  def submit
    session[:order_id] = params[:order_id]
    @order = Order.find(session[:order_id])
    @order.total = get_total
    books = Book.all
    books.each do |book|
      if session[:cart].include? book.id
        @order.books << book
      end
    end
    @order.save
    @books = Book.all
    @total = get_total
    @shipping_types = get_types_of_shipping(@order.country)
    puts @order.country
    if @order.country == 'United States'
      session[:shipping_type] = 'free'
    else
      session[:shipping_type] = 'international'
    end
    shipping_cart(session[:shipping_type])
    puts session[:shipping_type]
    puts session[:shipping_cost]
                                                                                   end

1 个答案:

答案 0 :(得分:0)

Started GET "/shipping_cart?shipping_type=priority&remote=true" for 127.0.0.1 at 2014-07-                 14 10:49:56 +0300
Processing by OrdersController#shipping_cart as HTML
Parameters: {"shipping_type"=>"priority", "remote"=>"true"}
Rendered orders/_shipping_cart.html.haml (1.5ms)
Completed 200 OK in 5ms (Views: 4.1ms | ActiveRecord: 0.0ms)


Started GET "/order/get_total" for 127.0.0.1 at 2014-07-14 10:49:57 +0300
Processing by OrdersController#get_total as */*
Book Load (0.6ms)  SELECT "books".* FROM "books"
Completed 200 OK in 3ms (Views: 0.2ms | ActiveRecord: 0.6ms)

这是AJAX调用的日志。有些有趣的是,在不同的呼叫顺序中呼叫不一样。例如。在一个电话中有

Started GET "/shipping_cart?shipping_type=priority&remote=true" for 127.0.0.1 at 2014-07-                 14 10:49:56 +0300

然后

Started GET "/order/get_total" for 127.0.0.1 at 2014-07-14 10:49:57 +0300

但反过来又以其他顺序。

有通知。在Rails的日志中,所有呼叫都以Completed 200 OK结束。但在Chrome中它是304 Not Modified。少数人中有一人是200 OK(有效,但不对)。