/ Published in: Ruby
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.
The Ruby on Rails code needs to be in a controller.
The rhtml code needs to be in the index layout.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
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 -%>