【发布时间】:2017-11-01 20:42:18
【问题描述】:
我正在按照教程根据子域更改数据库,子域必须是系统将连接到的现有数据库的名称,如this tutorial 在 15:30 分钟所示,并且运行良好,当使用数据库不存在的子域访问系统时出现问题,显示以下错误:Cannot open database "another_database" requested by the login. The login failed
并且显示错误不允许访问系统,无论是否使用子域中现有数据库的名称访问它,除非重新启动rails服务器。
这是我在 ApplicationController 中的代码:
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_action :connect_to_database
def connect_to_user_database(name)
config = ActiveRecord::Base.configurations["development"].merge("database" => "#{name}")
ActiveRecord::Base.establish_connection(config)
#auth = name
#raise auth.to_yaml #podemos ver los datos que nos ofrece el parametro
end
private
def connect_to_database
connect_to_user_database(request.subdomains(0).first)
end
end
【问题讨论】:
-
为了安全和避免错误,您应该在
connect_to_database之前检查子域是否有效。 -
@wesley6j 感谢您的回答,但是如何检查数据库是否存在?
-
如果您有想要使用的子域列表,请将数组保存在代码库或配置文件中的某个位置。
-
你能
rescue StandardError render file: "#{Rails.root}/public/404", layout: true, status: :not_found end -
@wesley6j 非常感谢,它成功了
标签: ruby-on-rails ruby database ruby-on-rails-4