簡體   English   中英

購物車:使用Rails / Ajax處理無產品錯誤

[英]Shopping cart : handling no product error with rails / ajax

當用戶將產品添加到購物車時,我正在使用ajax更改顯示的購物車。 為此,我用:remote => true調用正確的控制器,該控制器會發送回更新的購物車。 這樣做的控制器方法如下:

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.html { redirect_to(magasin_url) }
    format.js   {@current_item = @line_item }
    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

結束

我希望能夠通過發送通知消息而不是更新購物車來處理空庫存。

我嘗試了這個:

if product.stock_to_display <=0
  respond_to do |format|
    format.html { }
    format.js   {}
  end
end

但是我不知道該用什么格式。 而且我不知道如何在rjs中執行條件:

    page.replace_html('panier', render(@cart))

page[:panier].visual_effect :blind_down if @cart.total_items == 1

page[:current_item].visual_effect   :highlight,
                                    :startcolor => "#88ff88",
                                    :endcolor => "#114411"

page.select("#notice").each {|notice| notice.hide}

所以我想知道如何使用ajax / rjs處理錯誤。 謝謝

好了,經過一番嘗試,我設法做了一些看起來很干凈的東西。

我在控制器中創建了一個@success布爾值,並影響了他一個值,之后我可以在RJS中使用布爾值來做我的工作或顯示通知。 這是我的新RJS。

if @success
  page.replace_html('panier', render(@cart))
  page[:panier].visual_effect :blind_down if @cart.total_items == 1
  page[:current_item].visual_effect     :highlight,
                                        :startcolor => "#88ff88",
                                        :endcolor => "#114411"
  page.select("#notice").each {|notice| notice.hide}
else
  page.replace_html :notice, flash[:notice]
  flash.discard
end

如果顯示屏出現javascript錯誤,請檢查應用程序布局,因為它可能會受到“如果通知”條件的限制

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM