我对 Rails 比较陌生,这是我的情况:
我正在使用 Rails 构建一个库存管理应用程序,以帮助一家公司的三个独立分支机构管理他们自己的产品库存。
这三个分支中的每一个都在跟踪相同的产品,使用相同的数据模型,但分别进行管理。我的计划是使用单个数据库构建一个应用程序,但该应用程序会跟踪所有三个分支机构的库存。
我的计划是这样的:
branch1.inventoryapp.com
branch2.inventoryapp.com
branch3.inventoryapp.com
每个子域都将指向具有相同功能和基本相同 View 的相同界面。唯一的区别是他们库存的实际内容,这将是当时实际在该分支机构的产品列表。
我可以使用 Rails 子域路由来做到这一点吗?
我应该为每个分支设置单独的 Controller 吗?
我应该使用 Controller namespace 吗?嵌套资源?
提前致谢!
最佳答案
在您的 application_controller 中,只需:
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
作为 View 中的助手。
关于ruby-on-rails - rails : Dividing up a single database between multiple subdomains,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6541576/