新手问题 关于图片链接出现两次 request 的问题

agnovo_wl · 2013年07月21日 · 最后由 agnovo_wl 回复于 2013年07月22日 · 3198 次阅读

各位大牛:

在学习《web 开发敏捷之道》第四版时,遇到一个问题,请见截图。

添加图片的链接时,似乎同时给 server 发了两条请求, 一条 post 请求,生成新的 line_item; 另一条请求,反馈 line_item index 页面。

此现象是在添加并 bundle 了,gem 'will_paginate','>=3.0.pre' 出现,不知两者是否有影响呀?

谢谢大家啦。

post 完 redirect 了,浏览器就再发个请求到转向后的页面,很正常吧...

在点击 photo 的 link 后,发出的 post request 得到正常的反应了,但在 line_items_controller 的 create action 里已无 redirect。 而且我的 photo 的链接,添加了 :remote=>true,作为 ajax 的 request,应该直接相应 create.js.rjs 才对呀? 但实际情况是,create 和 index 均得到相应,create 在先,可以看见 cart 里有 line_item 增加,index 在后(line_item 新增后立刻跳转至 index)。 求解哦...

贴下日志看看:

tail -n 100 log/development.log

点击图片链接 1 次后,出现两个 requirest, log 信息如下,请大家多帮帮忙呀: Started POST "/line_items?product_id=2" for 127.0.0.1 at Mon Jul 22 06:13:10 +0800 2013 Processing by LineItemsController#create as JS Parameters: {"product_id"=>"2"} [1m[36mCart Load (0.0ms)[0m [1mSELECT "carts".* FROM "carts" WHERE "carts"."id" = 37 LIMIT 1[0m [1m[35mProduct Load (0.0ms)[0m SELECT "products".* FROM "products" WHERE "products"."id" = 2 ORDER BY title LIMIT 1 [1m[36mLineItem Load (0.0ms)[0m [1mSELECT "line_items".* FROM "line_items" WHERE "line_items"."product_id" = 2 AND ("line_items".cart_id = 37) LIMIT 1[0m [1m[35mCACHE (0.0ms)[0m SELECT "products".* FROM "products" WHERE "products"."id" = 2 ORDER BY title LIMIT 1 [1m[36mAREL (0.0ms)[0m [1mINSERT INTO "line_items" ("product_id", "created_at", "order_id", "quantity", "cart_id", "product_price", "updated_at") VALUES (2, '2013-07-21 22:13:11.151218', NULL, 1, 37, 49.5, '2013-07-21 22:13:11.151218')[0m [1m[35mSQL (15.6ms)[0m SELECT COUNT() FROM "line_items" WHERE ("line_items".cart_id = 37) [1m[36mLineItem Load (0.0ms)[0m [1mSELECT "line_items". FROM "line_items" WHERE ("line_items".cart_id = 37)[0m [1m[35mProduct Load (0.0ms)[0m SELECT "products".* FROM "products" WHERE "products"."id" = 2 ORDER BY title LIMIT 1 Rendered line_items/_line_item.html.erb (15.6ms) Rendered carts/_cart.html.erb (46.8ms) [1m[36mSQL (0.0ms)[0m [1mSELECT SUM("line_items"."quantity") AS sum_id FROM "line_items" WHERE ("line_items".cart_id = 37)[0m Rendered line_items/create.js.rjs (78.0ms) Completed 200 OK in 624ms (Views: 187.2ms | ActiveRecord: 15.6ms)

Started GET "/line_items?product_id=2" for 127.0.0.1 at Mon Jul 22 06:13:11 +0800 2013 Processing by LineItemsController#index as HTML Parameters: {"product_id"=>"2"} [1m[35mLineItem Load (0.0ms)[0m SELECT "line_items".* FROM "line_items" Rendered line_items/index.html.erb within layouts/application (93.6ms) Completed 200 OK in 187ms (Views: 140.4ms | ActiveRecord: 0.0ms)

补充一下:问题只有在使用 ie 时出现。 我使用 chrom 浏览器的时候,点击图片链接,仅出现 post 的 request; 而且,在使用 gem 'will_paginate','>=3.0.pre' 之前,在 IE 里用的也是正常的:(

#3 楼 @hicupp 目测楼主是 win 这个 tail 恐怕不成

以前做过的一个 demo 的LineItemsController代码,ajax 改用.js.erb,没有用rjs。楼主参考下?

def create
  @cart = current_cart
  product = Product.find(params[:product_id])
  @line_item = @cart.add_product(product.id)

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

@hicupp controller 里没有问题,和你的是一样样的,但是莫名其妙的是 photo 增加了 index 请求。 class LineItemsController < ApplicationController # GET /line_items # GET /line_items.xml def index @line_items = LineItem.all respond_to do |format| format.html # index.html.erb format.xml { render :xml => @line_items } end end

def create

@cart=current_cart product=Product.find(params[:product_id]) @line_item=@cart.add_product(product.id)

respond_to do |format| if @line_item.save session[:count]=0 format.js {@current_item=@line_item} format.html { redirect_to(store_url) } format.xml { render :xml => @line_item, :status => :created, :location => @line_item } else format.html { render :action => "new" } format.xml { render :xml => @line_item.errors, :status => :unprocessable_entity } end end end

需要 登录 后方可回复, 如果你还没有账号请 注册新账号