公寓:TenantNotFound - 以下架构之一无效

Apartment: TenantNotFound - One of the following schema(s) is invalid

我第一次在 Rails 项目的 Ruby 中使用 Apartment Gem 进行多租户。我正在尝试为您的数字图书馆 Rails 应用程序中的用户创建多个租户。

我正在使用 Devise Gem 对应用程序进行身份验证,并且我已经通过 运行 终端中的以下代码生成了配置文件

rails generate devise:install

我还在我的终端

中使用下面的代码为Devise生成了一个User模型
rails generate devise User

对于 公寓 Gem,我已经安装了它并通过 运行 在我的终端

下面的代码生成了配置文件]
bundle exec rails generate apartment:install

我还使用提供的文档根据需要配置了 config/initializers/apartment.rb 初始化程序文件,并且我已经通过我的注册页面在

创建了一个新租户
localhost:3000/users/sign_up

但是当我尝试在

查看新创建的租户时
http://newtenant.lvh.me:3000/

我收到以下错误

Apartment::TenantNotFound(One of the following schema(s) is invalid: "" "public")

我试图找出问题的原因,但我仍然没有幸运地修复它。我需要协助。提前谢谢你。

这是我修复它的方法

我只是在我的多租户子域模型中添加了一个名为 create_tenant 的 after_create 回调,即 app/models/user.rb[=21= 中的 user.rb ]

class User < ApplicationRecord
  after_create :create_tenant

  private

  def create_tenant
    Apartment::Tenant.create(subdomain)
  end
end 

如果您使用 Devise 进行身份验证,请使用此选项

class User < ApplicationRecord
  after_create :create_tenant

  # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :validatable

  private

  def create_tenant
    Apartment::Tenant.create(subdomain)
  end
end

N/B: 我租户的型号名称是 User,您的型号名称可能不同,所以请检查您的 app/models/ 查看您用于 多租户

的模型名称

您可以再次尝试创建新租户,一切都应该 运行 没问题。

下面的建议可能仅适用于那些使用 Devise 进行身份验证的人或所有其他使用身份验证系统的人

如果您运行遇到这样的任何问题

Filter chain halted as :require_no_authentication rendered or redirected

在尝试修复 找不到租户 的问题时,只需使用以下代码重新启动本地服务器

rails server

然后访问下面的 link 来编辑您新创建的 users/tenant

localhost:3000/users/edit

N/B: localhost:3000/users/edit中的users代表我用的模型的复数名称(table)对于我的多租户。

当页面打开时,只需 Delete/Cancel 帐户 然后您已经创建的帐户。

现在您可以再次尝试创建新租户,一切都应该 运行 没问题。

就这些了。

希望对您有所帮助