You have to handle the error using a rescue
block:
def search
@users = User.find_using_term params[:query]
rescue ActiveSearch::EmptyTermError
flash[:notice] = "No Results Found"
redirect_to :action => "home"
end
That way, though, if the search ran fine, but the result set is still empty, you won't be redirected. Here's some code that flashes and redirects you for both empty results and an EmptyTermError.
def search
@users = User.find_using_term params[:query]
return unless @users.empty?
rescue ActiveSearch::EmptyTermError
ensure
flash[:notice] = "No Results Found"
redirect_to :action => "home"
end
The return
just leaves if we've got results.
The rescue
block is empty, it just lets the error pass through.
And the ensure
block runs whether we've handled an exception or not. (But certainly not if we returned before it.)
kch
2009-05-06 22:19:58