#2 楼 @zhaojiahui 楼主,不知你的问题解决没有呀?我在这也遇到问题了,不过我的问题是,点击 button_to 后,会产生 js 请求(属正常反应),还会产生 html 的 post 请求(非正常的反应),所以 cart 的内容会增加 2。郁闷中 ......
@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
补充一下:问题只有在使用 ie 时出现。 我使用 chrom 浏览器的时候,点击图片链接,仅出现 post 的 request; 而且,在使用 gem 'will_paginate','>=3.0.pre' 之前,在 IE 里用的也是正常的:(
点击图片链接 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)
在点击 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)。 求解哦...
看来,求人不如求己啊,要自己整啦...
呵呵,整篇里面 500 成了绝对的亮点
拍砖归拍砖,“言”还是要正传才能实归哦。 此项目简单,熟练的工程师 1-2 天应该能做成,但是前期的需求沟通需要花些时间; 目前的 C/S 系统已有,只是用户界面不佳,需改善(甚至全改); 另外和多系统的铰链是一个挑战。
可进行邮箱联系:[email protected]