捕获要在重定向页上显示的错误
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了捕获要在重定向页上显示的错误相关的知识,希望对你有一定的参考价值。
Catches an exception error, uses a technique called flash to display error on another page.The Ruby on Rails code needs to be in a controller.
The rhtml code needs to be in the index layout.
def add_to_cart begin product = Product.find(params[:id]) # if can not find product id, catch exception, raise error to be # displayed on 'index' page rescue ActiveRecord::RecordNotFound logger.error("Atempt to access invalid product #{params[:id]}") flash[:notice] = "invalid product" redirect_to :action => :index else # if can find product id, continue to add product to cart @cart = find_cart @cart.add_product(product) end end # RHTML code <% if flash[:notice] -%> <div id="notice"><%= flash[:notice] %></div> <% end -%>
以上是关于捕获要在重定向页上显示的错误的主要内容,如果未能解决你的问题,请参考以下文章
spring security 在重定向到 logout.jsp 时给出错误