新手问题 Rails 异常处理

zorrodjn · March 19, 2014 · Last by hozaka replied at March 20, 2014 · 3732 hits

照着《应用 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

恳请大神指教,先谢过了!

哪位哥哥帮忙看看啊?

记得把代码排整齐吧。

你是用 rails g scaffold 做的框架吧?应该有一个 set_cart 方法,异常是那里抛的。

BTW,你 trace 不贴完整别人怎么帮你看……

You need to Sign in before reply, if you don't have an account, please Sign up first.