看看这个项目我工作:https://github.com/fabiob/guildhost-hosting
您所需要的代码是在app/controllers/application_controller.rb
:
class ApplicationController < ActionController::Base
protect_from_forgery
before_filter :load_subdomain
def load_subdomain
@subdomain = self.request.subdomains[0] || 'local'
load_customer
raise "Invalid Subdomain: #{self.request.subdomains}" unless @subdomain.present?
end
def load_customer
@customer = Customer.find(@subdomain)
end
end
而且在app/views/layouts/application.html.erb
:
<!DOCTYPE html>
<html>
<head>
<title><%= @customer.name %> @ MySaaS.com.br</title>
<%= stylesheet_link_tag :all %>
<%= stylesheet_link_tag "/themes/#{@subdomain}/main.css" %>
<%= javascript_include_tag :defaults %>
<%= csrf_meta_tag %>
</head>
<body>
...
</body>
</html>
有一个/public/themes
其中驻留有每个子域的定制的文件夹。每个子域有机会定制一个CSS,main.css
。如果需要多个CSS文件,则应在main.css
上使用@import
条款。
谢谢,因为我们现在有过滤器标签,我可以以某种方式退出每个动作找到一个全局ID? @accountid = Account.find_by_subdomain!(request.subdomain).id – tspore 2010-11-19 21:50:03