use*_*191 11 ruby ruby-on-rails-3 apartment-gem
我正在使用这间公寓一颗红宝石.
我在application.rb文件中添加了这个:
config.middleware.use 'Apartment::Elevators::Subdomain'
Run Code Online (Sandbox Code Playgroud)
当我尝试在浏览器url'test.domain.local:3000'中点击此内容时,PostgreSQL中不存在子域'test'架构,我看到此错误
Apartment::SchemaNotFound (One of the following schema(s) is invalid: test, "public")
Run Code Online (Sandbox Code Playgroud)
我知道这是gem的正常行为但是想要捕获这个异常并将用户重定向到其他页面,我该怎么做?
Rac*_*oon 14
这就是我做的:
在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
Run Code Online (Sandbox Code Playgroud)
并按以下行添加到您的Apartment初始化程序文件中:
require 'rescued_apartment_middleware'
Rails.application.config.middleware.use 'Apartment::Elevators::Subdomain'
Apartment::Elevators::Subdomain.prepend RescuedApartmentMiddleware
Run Code Online (Sandbox Code Playgroud)
这就像一个魅力!(用ruby 2.1和Rails 4.1测试)