2014-11-28 13 views
10

Sto usando questa gemma rubino .Appartamento rubino gemma: Vuoi prendere un'eccezione

devo aggiungere questo in application.rb di file:

config.middleware.use 'Apartment::Elevators::Subdomain' 

quando provo colpire questo browser url 'test.domain.local: 3000', dove sottodominio 'test' schema non esiste in PostgreSQL , vedo questo errore

Apartment::SchemaNotFound (One of the following schema(s) is invalid: test, "public") 

so che questo è un comportamento normale del gioiello, ma voglio prendere questa eccezione e reindirizzare utente a qualche altra pagina, come posso farlo?

risposta

12

Questo è quello che ho fatto:

Creare file in lib/rescued_apartment_middleware.rb

module RescuedApartmentMiddleware 
    def call(*args) 
    begin 
     super 
    rescue Apartment::TenantNotFound 
     Rails.logger.error "ERROR: Apartment Tenant not found: #{Apartment::Tenant.current.inspect}" 
     return [404, {"Content-Type" => "text/html"}, ["#{File.read(Rails.root.to_s + '/public/404.html')}"] ] 
    end 
    end 
end 

e aggiungere al vostro appartamento di file di inizializzazione seguenti linee:

require 'rescued_apartment_middleware' 

Rails.application.config.middleware.use 'Apartment::Elevators::Subdomain' 
Apartment::Elevators::Subdomain.prepend RescuedApartmentMiddleware 

Questo funziona come un fascino! (Testato con Ruby 2.1 e Rails 4.1)

+4

FYI 'Appartamento :: DatabaseNotFound, Apartment :: SchemaNotFound' può essere sostituito con' Apartment :: TenantNotFound'. Queste altre eccezioni sono deprecate. – brad