Rails: Dividing up a single database between multiple subdomains

两盒软妹~` 提交于 2019-12-04 12:53:32
Jesse Wolgamott

In your application_controller, simply:

class ApplicationController < ActionController::Base
  before_filter :current_account
  helper_method :current_account

  def current_account
    @account ||= Account.find_by_domain(request.subdomain)
  end
end

Then, everywhere else:

class WidgetsController < ApplicationController

  def index
    @widgets = current_account.widgets.paginate(:page=>params[:page])
  end

  def show
    @widget = current_account.widgets.find(params[:id])
  end

  def create
    @widget = current_account.widgets.build(params[:widget])
    if @widget.save
      ...
    end
  end
end

By scoping everything to @account, you are keeping data separate across the subdomains.

You can also use current_account as a helper in your views.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!