Rails:在多个子域之间划分单个数据库 [英] Rails: Dividing up a single database between multiple subdomains
问题描述
我是Rails的新手,这里是我的情况:
我建立一个库存管理应用程序,使用rails帮助公司的三个单独的分支管理自己的产品库存。
这三个分支都跟踪相同的产品,使用相同的数据模型,但是分别管理。我的计划是使用单个数据库构建单个应用程序,但记录所有三个分支中的库存。
我的计划是这样:
branch1.inventoryapp.com
branch2.inventoryapp.com
branch3.inventoryapp.com
每个子域将导致具有相同功能和基本相同视图的相同界面。唯一的区别是其广告资源的实际内容,这是当时该分支机构的实际产品列表。
我可以做这与rails子域路由?
我应该使用控制器命名空间?嵌套资源?
提前感谢!
,只需:
class ApplicationController< ActionController :: Base
before_filter:current_account
helper_method:current_account
def current_account
@account || = Account.find_by_domain(request.subdomain)
end
end
然后,其他地方:
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
通过将所有内容范围限定为 @account
,您可以在子域之间保持数据分隔。
您还可以使用 current_account
作为视图中的帮助器。
I'm relatively new to Rails, and here is my situation:
I'm building an inventory management app with rails to help three separate branches of a company manage their own product inventory.
Each of these three branches are keeping track of the same products, use the same data models, but are managed separately. My plan is to build a single app using a single database, but one that keeps track of the inventory in all three branches.
My plan is to have something like this:
branch1.inventoryapp.com
branch2.inventoryapp.com
branch3.inventoryapp.com
Each subdomain will lead to the same interface with the same functions and essentially the same views. The only difference will be the actual content of their inventory, which will be a list of products that are physically at that branch at the time.
Will I be able to do this with rails subdomain routing?
Should I have separate controllers for each branch?
Should I use controller namespaces? Nested resources?
Thanks in advance!
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.
这篇关于Rails:在多个子域之间划分单个数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!