繁体   English   中英

rails中的子域域管理

[英]Subdomain domain managment in rails

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

只有这个克隆的专用数据库

这就是多租户 - 真正的多租户是您拥有多个数据库的地方 - 每个用户通过一个应用程序实例运行一个数据库。

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

有一些宝石 - 比如Apartment - 它允许PGSQL范围内的一些多租户功能。 有一个关于这个Railscast 这里

在此输入图像描述

这仅适用于Postgres。 如果您正在使用MYSQL,则每次注册新用户时都必须创建一种加载,填充和引用各个表的方法。 不是一个平凡的壮举。


为用户制作此应用程序的克隆,并为每个应用程序创建一个子域

你没有克隆应用程序; 您需要使用一个应用程序实例,然后将其与多数据孤岛一起使用。

关于子域的另一个很棒的Railscast

在此输入图像描述

就子域而言,您必须构建流以处理不同的用户实例:

#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

这将使您能够拥有“欢迎”页面,管理用户登录,然后有一个中央“用户”区域,他们在子域中查看他们的帖子等。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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