In my program, I have to get product info using the product id, like this:
# Show the product info.
def show
@product = Product.find params[:id]
if !@product
redirect_to products_path, :alert => 'Product not found!.'
end
end
In case the product doesn’t exist, I have a Rails error Couldn’t find Product with id=xxx. I would like to override this error message and display the error by myself as a flash alert.
How can I do that? Thanks in advance.
You can write
Product.try(:find, params[:id])orProduct.find_by_id params[:id]and get nil in case the product is not found instead of an exception