照着《应用 Rails 进行敏捷开发》做一个购物车系统,处理异常的 action 不能如期工作。
app/controllers/carts_controller.rb路径下修改show方法如下:
# GET /carts/1 # GET /carts/1.json def show begin @cart = Cart.find(params[:id]) rescue ActiveRecord::RecordNotFound logger.error "Attempted to access invalid cart #{params[:id]}" redirect_to store_url, :notice => 'Invalid cart' else respond_to do |format| format.html # show.html.erb format.xml { render :xml => @cart} end end end
之后在浏览器输入:http://localhost:3000/carts/asdfasdfadt提示错误 ActiveRecord::RecordNotFound in CartsController#show Couldn't find Cart with id=asdfasdfad
恳请大神指教,先谢过了!