Return to Snippet

Revision: 2472
at February 26, 2007 18:22 by wackimonki


Updated Code
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 -%>

Revision: 2471
at February 26, 2007 17:58 by wackimonki


Initial Code
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

Initial URL

                                

Initial Description
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.

Initial Title
Catching Error to be Displayed on Redirected Page

Initial Tags
rails

Initial Language
Ruby