问题
I am using the Apartment Gem for the first time for Multitenancy in a Ruby on Rails project. I am trying to create multiple tenants for users in your digital library Rails application.
I am using Devise Gem for the authentication of the application, and I have generated the and I have generated config file by running the code below in my terminal
rails generate devise:install
I have also generated a User model for Devise using the code below in my terminal
rails generate devise User
And for the Apartment Gem, I have installed it and generated the config file by running the code below in my terminal
bundle exec rails generate apartment:install
I have also configured the config/initializers/apartment.rb
initializer file as needed using the documentation provided, and I have created a new tenant via my signup page at
localhost:3000/users/sign_up
But when I try viewing the newly created tenant at
http://newtenant.lvh.me:3000/
I get the error below
Apartment::TenantNotFound(One of the following schema(s) is invalid: "" "public")
I have tried to figure out the cause of the issue, but I still haven't been fortunate to get it fixed. I need some help. Thank you in advance.
回答1:
Here's how I fixed it
I simply added an after_create callback called create_tenant
to the model of the subdomain for my multi-tenant, which is user.rb
in app/models/user.rb
class User < ApplicationRecord
after_create :create_tenant
private
def create_tenant
Apartment::Tenant.create(subdomain)
end
end
Use this if you used Devise for Authentication
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: My model name for the Tenant is User, your model name may be different, so check through your app/models/
to see the model name that you used for the multi-tenant
You can try creating new tenants again, and everything should run fine.
This advice below may apply to only those who used Devise for authentication or to every other person using an authentication system
If you run into any issue like this
Filter chain halted as :require_no_authentication rendered or redirected
when trying to fix this issue of Tenant Not Found, simply restart your local server by using the code below
rails server
and then visit the link below to edit your newly created users/tenant
localhost:3000/users/edit
N/B: The users
in localhost:3000/users/edit
represents the plural name (table) of the model that I used for my multi-tenant.
When the page opens up, simply Delete/Cancel the Account and then that you've created already.
Now you can try creating new tenants again, and everything should run fine.
That's all.
I hope this helps
来源:https://stackoverflow.com/questions/57771337/apartment-tenantnotfound-one-of-the-following-schemas-is-invalid