I am using ajax to change the displayed cart as the user add product to cart. To do so i call with :remote=>true the correct controller that send back the updated cart. The controller method to do so is the following :
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.sa开发者_运维问答ve
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
end
I'd like to be able to handle an empty stock by sending a notice message instead of updating the cart.
I tried this :
if product.stock_to_display <=0
respond_to do |format|
format.html { }
format.js {}
end
end
But i do not know what to put in the format. and i have no idea how to do a condition in the 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}
So i'd like to know how to handle the error with ajax/rjs. Thanks
Ok after a little bit of try i managed to do something that seems clean.
I created a @success boolean in the controller and affected him a value, after that i can use the boolean in RJS to either do my stuff or display the notice. Here is my new 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
If you have a javascript error with the display, check the application layout as it may be restricted by a "if notice" condition
精彩评论