2015-10-08 65 views
3

现在我有一个应用程序,其中包括大量的东西,如狂欢,炼油厂,论坛和许多其他宝石。所以我需要为用户制作这个应用程序的副本,并为每个应用程序创建一个子域。像user1.mydomain.com这导致克隆我的应用与专用数据库只为这​​个克隆。所以现在我刚刚复制并粘贴了文件夹,但是这是一个非常糟糕的做法,我遇到了很多问题。所以我的问题是。我怎样才能实现这个?或者我的麻烦可能是特殊的宝石?栏中的子域管理

回答

1

专用DB只为这个克隆

这是一种叫multi tenancy - 真正多租户就是你有多个数据库的 - 一个是通过一个应用程序实例运行的每个用户。

对于Rails来说,这是一个非常技术性的问题,因为它之前没有做过。

有宝石 - 如Apartment - 这允许与PGSQL scoping一些多租户功能。有一个Railscast这个here

enter image description here

与Postgres的这只作品。如果你使用的是MYSQL,你必须创建一种加载方式,每次注册一个新用户时填充&引用单个表。不是一个意思壮举。


使这个应用程序为用户的一个克隆,并为每个

子域你不是制作克隆应用的;您需要使用一个应用程序实例,然后您将使用该实例与多个数据孤岛。

还有一个很大的Railscast about the subdomains here

enter image description 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 

这会给你有能力拥有一个“欢迎”页面,管理用户登录,然后有一个中央“用户”区域,他们在他们的子域内看到他们的帖子等。