【发布时间】: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