javascript - AJAX 在手动页面刷新之前未呈现更新

标签 javascript jquery ruby-on-rails ajax

我正在使用 Rails 4 进行敏捷 Web 开发,一直停留在第 11 章,迭代 F4 上。该部分的目标是仅当购物车中有商品时才显示购物车 div,否则通过 display: none 隐藏它。单击“清空购物车”按钮时,购物车会正确隐藏自身,但是当向其中添加商品时,除非手动刷新页面,否则购物车不会显示。

以下是将商品添加到空购物车时服务器输出的样子:

Started POST "/line_items?product_id=2" for ::1 at 2015-05-10 18:51:44 -0700
Processing by LineItemsController#create as JS
  Parameters: {"authenticity_token"=>"v2zcRr2CPsfZP3/qI8l5m0HWdDoOiiyl5oiZxvpYKXp7K2ecXizzCZA37DLm7PwYuSAgemogwjjnDHz4NbavGA==", "product_id"=>"2"}
  Cart Load (0.2ms)  SELECT  "carts".* FROM "carts" WHERE "carts"."id" = ? LIMIT 1  [["id", 21]]
  Product Load (0.1ms)  SELECT  "products".* FROM "products" WHERE "products"."id" = ? LIMIT 1  [["id", 2]]
  LineItem Load (0.1ms)  SELECT  "line_items".* FROM "line_items" WHERE "line_items"."cart_id" = ? AND "line_items"."product_id" = ? LIMIT 1  [["cart_id", 21], ["product_id", 2]]
   (0.1ms)  begin transaction
  SQL (0.4ms)  INSERT INTO "line_items" ("product_id", "cart_id", "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["product_id", 2], ["cart_id", 21], ["created_at", "2015-05-11 01:51:44.341300"], ["updated_at", "2015-05-11 01:51:44.341300"]]
   (1.1ms)  commit transaction
  LineItem Load (0.2ms)  SELECT "line_items".* FROM "line_items" WHERE "line_items"."cart_id" = ?  [["cart_id", 21]]
  Product Load (0.2ms)  SELECT  "products".* FROM "products" WHERE "products"."id" = ? LIMIT 1  [["id", 2]]
  Rendered line_items/_line_item.html.erb (2.0ms)
  Rendered carts/_cart.html.erb (6.7ms)
  Rendered line_items/create.js.erb (8.9ms)

这是我刷新时的输出:

Started GET "/" for ::1 at 2015-05-10 18:57:17 -0700
Processing by StoreController#index as HTML
  Cart Load (0.1ms)  SELECT  "carts".* FROM "carts" WHERE "carts"."id" = ? LIMIT 1  [["id", 21]]
  Product Load (2.2ms)  SELECT  "products".* FROM "products"  ORDER BY "products"."updated_at" DESC LIMIT 1
  Product Load (0.2ms)  SELECT "products".* FROM "products"  ORDER BY "products"."title" ASC
  Rendered store/index.html.erb within layouts/application (17.9ms)
  LineItem Exists (0.2ms)  SELECT  1 AS one FROM "line_items" WHERE "line_items"."cart_id" = ? LIMIT 1  [["cart_id", 21]]
  LineItem Load (0.2ms)  SELECT "line_items".* FROM "line_items" WHERE "line_items"."cart_id" = ?  [["cart_id", 21]]
  Product Load (0.1ms)  SELECT  "products".* FROM "products" WHERE "products"."id" = ? LIMIT 1  [["id", 2]]
  Rendered line_items/_line_item.html.erb (1.6ms)
  Rendered carts/_cart.html.erb (6.2ms)
Completed 200 OK in 129ms (Views: 124.7ms | ActiveRecord: 3.0ms)

这是我的 LineItemsController,其中包含将商品添加到空购物车时触发的 create 操作:

class LineItemsController < ApplicationController
  include CurrentCart
  before_action :set_cart, only: [:create]
  before_action :set_line_item, only: [:show, :edit, :update, :destroy]

  # GET /line_items
  # GET /line_items.json
  def index
    @line_items = LineItem.all
  end

  # GET /line_items/1
  # GET /line_items/1.json
  def show
  end

  # GET /line_items/new
  def new
    @line_item = LineItem.new
  end

  # GET /line_items/1/edit
  def edit
  end

  # POST /line_items
  # POST /line_items.json
  def create
    product = Product.find(params[:product_id])
    @line_item = @cart.add_product(product.id)

    respond_to do |format|
      if @line_item.save
        format.html { redirect_to store_url }
        format.js { @current_item = @line_item }
        format.json { render action: 'show', status: :created, location: @line_item }
      else
        format.html { render action: 'new' }
        format.json { render json: @line_item.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /line_items/1
  # PATCH/PUT /line_items/1.json
  def update
    respond_to do |format|
      if @line_item.update(line_item_params)
        format.html { redirect_to @line_item, notice: 'Line item was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @line_item.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /line_items/1
  # DELETE /line_items/1.json
  def destroy
    @line_item.destroy
    respond_to do |format|
      format.html { redirect_to line_items_url }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_line_item
      @line_item = LineItem.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def line_item_params
      params.require(:line_item).permit(:product_id)
    end
end

这是我的 StoreController,包含手动刷新触发的 index 操作:

class StoreController < ApplicationController
  include CurrentCart
  before_action :set_cart

  def index
    @products = Product.order(:title)
  end
end

似乎当我清空购物车并检查 div 时,会从 application.html.erb 应用 display: none 属性(应该如此):

<!DOCTYPE html>
<html>
<head>
  <title>Pragprog Books Online Store</title>
  <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %>
  <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
  <%= csrf_meta_tags %>
</head>
<body class='<%= controller.controller_name %>'>
  <div id="banner">
    <%= image_tag("logo.png") %>
    <%= @page_title || "Pragmatic Bookshelf" %>
  </div>
  <div id="columns">
    <div id="side">
      <%= hidden_div_if(@cart.line_items.empty?, id: 'cart') do %>
        <%= render @cart %>
      <% end %>

      <ul>
        <li><a href="http://www....">Home</a></li>
        <li><a href="http://www..../faq">Questions</a></li>
        <li><a href="http://www..../news">News</a></li>
        <li><a href="http://www..../Contact">Contact</a></li>
      </ul>
    </div>
    <div id="main">
      <%= yield %>
    </div>
  </div>
</body>
</html>

hidden_​​div_if 方法位于 ApplicationHelper.rb 中:

module ApplicationHelper
  def hidden_div_if(condition, attributes = {}, &block)
    if condition
      attributes["style"] = "display: none"
    end
    content_tag("div", attributes, &block)
  end
end

但是当我在添加项目后检查购物车 div 时,仍然应用 display: none 。我错过了什么?

如果重要的话,这是我的 CartsController:

class CartsController < ApplicationController
  before_action :set_cart, only: [:show, :edit, :update, :destroy]
  rescue_from ActiveRecord::RecordNotFound, with: :invalid_cart

  # GET /carts
  # GET /carts.json
  def index
    @carts = Cart.all
  end

  # GET /carts/1
  # GET /carts/1.json
  def show
  end

  # GET /carts/new
  def new
    @cart = Cart.new
  end

  # GET /carts/1/edit
  def edit
  end

  # POST /carts
  # POST /carts.json
  def create
    @cart = Cart.new(cart_params)

    respond_to do |format|
      if @cart.save
        format.html { redirect_to @cart, notice: 'Cart was successfully created.' }
        format.json { render :show, status: :created, location: @cart }
      else
        format.html { render :new }
        format.json { render json: @cart.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /carts/1
  # PATCH/PUT /carts/1.json
  def update
    respond_to do |format|
      if @cart.update(cart_params)
        format.html { redirect_to @cart, notice: 'Cart was successfully updated.' }
        format.json { render :show, status: :ok, location: @cart }
      else
        format.html { render :edit }
        format.json { render json: @cart.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /carts/1
  # DELETE /carts/1.json
  def destroy
    @cart.destroy if @cart.id == session[:cart_id]
    session[:cart_id] = nil
    respond_to do |format|
      format.html { redirect_to store_url }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_cart
      @cart = Cart.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def cart_params
      params[:cart]
    end
    def invalid_cart
      logger.error "Attempt to access invalid cart #{params[:id]}"
      redirect_to store_url, notice: 'Invalid cart'
    end
end

最佳答案

经过几天的努力,我终于注意到 create.js.erb 中的 jQuery 选择器缺少 #,即:

if ($('cart tr').length == 1) {
  $('cart').show('blind', 1000);
}

而不是:

if ($('#cart tr').length == 1) {
  $('#cart').show('blind', 1000);
}

添加它们修复了它。

关于javascript - AJAX 在手动页面刷新之前未呈现更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30158897/

相关文章:

javascript - 动态添加文本框输入将作为一个保存到数据库中

javascript - height=100% 不渲染 google maps api

javascript - jquery 在以下选择器中引用初始元素选择器

javascript - 使用 rails 中的 delete 方法单击 link_to 时创建自定义 jquery 确认对话框

javascript - 需要帮助查找整数数组中的逻辑缺陷以查找 K 个不同对的对

javascript - jQuery 周期2 : move through slides on scroll

javascript - 在父元素之后稍微淡化子元素

javascript - 使用 oncontextmenu 和 js/jquery 左键单击上下文菜单

ruby-on-rails - 使用回形针 gem 重新生成缩略图

ruby-on-rails - Rails 使用他的 getter 覆盖属性