12

我正在使用这间公寓作为红宝石。

我在 application.rb 文件中添加了这个:

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

当我尝试在 PostgreSQL 中不存在子域 'test' 模式的浏览器 url 'test.domain.local:3000' 中点击这个时,我看到了这个错误

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

我知道这是 gem 的正常行为,但想捕获此异常并将用户重定向到其他页面,我该怎么做?

4

1 回答 1

16

这就是我所做的:

在 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

并将以下几行添加到您的公寓初始化程序文件中:

require 'rescued_apartment_middleware'

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

这就像一个魅力!(用 ruby​​ 2.1 和 Rails 4.1 测试)

于 2015-01-30T10:28:28.120 回答