简体   繁体   中英

Subdomain domain managment in rails

Right now I have for an app which includes lots of stuff like spree, refinery, forum and lots of other gems. so I need to make a clone of this app for for users and make a sub domain for each. like user1.mydomain.com which lead to clone of my app with dedicated db only for this clone. So right now I have just copied and paste folders but its a very very bad practice and I have run into many problems with this. so my question is. How can I implement this? or maybe special gems for my trouble?

with dedicated db only for this clone

This is something called multi tenancy - true multitenancy is where you have multiple DB's - one for each user running through one application instance.

This is a very technical point for Rails, as it's not been done before.

There are gems - such as Apartment - which allow some multi-tenant functionality with PGSQL scoping . There is a Railscast about this here :

在此输入图像描述

This only works with Postgres. If you're using MYSQL, you'd have to create a way to load, populate & reference individual tables each time you sign up a new user. Not a mean feat.


make a clone of this app for for users and make a sub domain for each

You're not making a clone of the app; you need to use one application instance, which you'll then use with multi data silos.

There is another great Railscast about the subdomains here :

在此输入图像描述

In terms of subdomains, you'll have to construct your flow to handle different user instances:

#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

This will give you the ability to have a "welcome" page, manage user logins, and then have a central "user" area where they see their posts etc within their subdomain.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM