2015-10-08 67 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 

這會給你有能力擁有一個「歡迎」頁面,管理用戶登錄,然後有一箇中央「用戶」區域,他們在他們的子域內看到他們的帖子等。