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

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

各位大牛:

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

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

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

谢谢大家啦。

共收到 8 条回复

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

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