现在我有一个应用程序,其中包含很多东西,例如狂欢、炼油厂、论坛和许多其他 gem 。所以我需要为用户克隆这个应用程序,并为每个用户创建一个子域。就像 user1.mydomain.com 一样,这导致我的应用程序克隆只有这个克隆的专用数据库。所以现在我只是复制和粘贴文件夹,但这是一个非常非常糟糕的做法,我遇到了很多问题。所以我的问题是。我该如何实现?或者可能是特殊的 gem 来解决我的麻烦?
最佳答案
with dedicated db only for this clone
这是一个叫做 multi tenancy 的东西- true Multi-Tenancy 是您拥有多个数据库的地方 - 一个用于运行一个应用程序实例的每个用户。
这对于 Rails 来说是一个非常技术性的问题,因为以前没有这样做过。
有 gem -比如Apartment
- 允许使用 PGSQL scoping 的一些 Multi-Tenancy 功能.关于这个有一个 Railscast here :
这只适用于 Postgres。如果您使用的是 MYSQL,则每次注册新用户时都必须创建一种方法来加载、填充和引用各个表。绝非易事。
make a clone of this app for for users and make a sub domain for each
您不是克隆应用;您需要使用一个应用程序实例,然后将其与多个数据孤岛一起使用。
还有一个很棒的Railscast about the subdomains here :
就子域而言,您必须构建流程来处理不同的用户实例:
#config/routes.rb
root "application#index"
constraints: Subdomain do
resources :posts, path: "" #-> user1.domain.com/ -> posts#index
end
#lib/subdomain.rb
class Subdomain
def matches?(request)
@users.exists? request.subdomain #-> would have to use friendly_id
end
end
#app/controllers/application_controller.rb
class ApplicationController < ApplicationController
def index
# "welcome" page for entire app
# include logic to determine whether use logged in. If so, redirect to subdomain using route URL
end
end
#app/controllers/posts_controller.rb
class PostsController < ApplicationController
before_action :set_user #-> also have to authenticate here
def index
@posts = @user.posts
end
private
def set_user
@user = User.find request.subdomain
end
end
这将使您能够拥有一个“欢迎”页面、管理用户登录,然后拥有一个中央“用户”区域,他们可以在其中查看其子域中的帖子等。
关于ruby-on-rails - Rails 中的子域域管理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33013901/