【问题标题】:Shopping cart : handling no product error with rails / ajax购物车:使用 rails / ajax 处理没有产品错误
【发布时间】:2011-10-14 04:15:43
【问题描述】:

当用户将产品添加到购物车时,我正在使用 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 处理错误。谢谢

【问题讨论】:

    标签: ruby-on-rails ajax error-handling


    【解决方案1】:

    好吧,经过一点尝试,我设法做了一些看起来很干净的事情。

    我在控制器中创建了一个@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 错误,请检查应用程序布局,因为它可能受到“if notice”条件的限制

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-02
      • 2012-06-29
      • 2015-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多