I have tried to create and use my own Exception class,
AmbigouosPlaceException.
I can’t seem to use my exception though, as I get the following
FRUSTRATING error…
Error
uninitialized constant AmbiguousPlaceException
C:/app/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.0/lib/active_support/dependencies.rb:89:in
const_missing' C:/app/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.0/lib/active_support/dependencies.rb:120:in
const_missing’
C:/app/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.0/lib/active_support/dependencies.rb:122:in
send' C:/app/ruby/lib/ruby/gems/1.8/gems/activesupport-1.3.0/lib/active_support/dependencies.rb:122:in
const_missing’
#{RAILS_ROOT}/app/models/search.rb:34:in search_by_category_and_place' #{RAILS_ROOT}/app/controllers/searches_controller.rb:22:in
search’
SearchesController
require ‘ambiguous_place_exception’
class SearchesController < ApplicationController
def search
create
category = @search.category_string
location = @search.location
if Postcode.validate(location)
@results = Search.search_by_category_and_postcode(category,
location)
else
begin
@results = Search.search_by_category_and_place(category,
location)
rescue AmbigouosPlaceException => e
@places = e.places
render :action => ‘choose_place’
end
end
end
end
AmbigouosPlaceException
class AmbigouosPlaceException < RuntimeError
attr :places
def initialize(places)
@places = places
end
end
Search
require ‘ambiguous_place_exception’
class Search < ActiveRecord::Base
def self.search_by_category_and_place(category, place)
# Find places by name
places = Place.find_all_by_name(place)
if places.size > 1
e = AmbiguousPlaceException.new (places)
raise e
end
...truncated...
end
end