如何通过Rails创建管理子域,实现子域的集中管理?

2026-04-11 19:531阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计559个文字,预计阅读时间需要3分钟。

如何通过Rails创建管理子域,实现子域的集中管理?

我使用的AuthLogic和本文中描述的子域方法运行良好,符合预期。我想探讨的是如何创建一个类似admin或host这样的子域,它将使用AuthLogic进行身份验证的用户。

我使用的是AuthLogic和 this blog post中涵盖的子域方法,一切都运行良好,并且符合预期.我想弄清楚的是如何创建一个像“admin”或“host”这样的子域,它将使用AuthLogic进行身份验证的用户(这可能是微不足道的,无需提及)来管理子域.基本上,所有子域都将正常运行,除了admin.site.com,它将转到自己的控制器和布局.

dhh建议只是引入一个例外来重定向,但是我不确定那里发生了什么,对我来说这似乎不是很简单,任何想法?

编辑
我认为我使用AuthLogic的事实在这里非常重要,因为子域逻辑不会在任何地方转发用户,一旦经过身份验证,AuthLogic会将用户发送到/ account – 所以我的问题可能与如何告知AuthLogic不同的地点有关user是root用户,登录admin子域.

这是我们迄今为止实现的代码

Company Model

class Company < ActiveRecord::Base has_many :users has_many :brands, :dependent => :destroy validates_presence_of :name, :phone, :subdomain validates_format_of :subdomain, :with => /^[A-Za-z0-9-]+$/, :message => 'The subdomain can only contain alphanumeric characters and dashes.', :allow_blank => true validates_uniqueness_of :subdomain, :case_sensitive => false validates_exclusion_of :format, :in => %w( support blog billing help api www host admin manage ryan jeff allie), :message => "Subdomain {{value}} is not allowed." before_validation :downcase_subdomain protected def downcase_subdomain self.subdomain.downcase! if attribute_present?("subdomain") end end

SubdomainCompanies Module

module SubdomainCompanies def self.included( controller ) controller.helper_method(:company_domain, :company_subdomain, :company_url, :company_account, :default_company_subdomain, :default_company_url) end protected # TODO: need to handle www as well def default_company_subdomain '' end def company_url( company_subdomain = default_company_subdomain, use_ssl = request.ssl? ) " : "") end end

Application Controller

class ApplicationController < ActionController::Base include SubdomainCompanies rescue_from 'Acl9::AccessDenied', :with => :access_denied helper :all # include all helpers, all the time protect_from_forgery # See ActionController::RequestForgeryProtection for details helper_method :current_user_session, :current_user, :current_company_name filter_parameter_logging :password, :password_confirmation before_filter :check_company_status protected def public_site? company_subdomain == default_company_subdomain end def current_layout_name public_site? ? 'public' : 'login' end def check_company_status unless company_subdomain == default_company_subdomain # TODO: this is where we could check to see if the account is active as well (paid, etc...) redirect_to default_company_url if current_company.nil? end end end 查看 subdomain-fu,它允许您根据子域路由到不同的控制器和操作.关于这个问题我做了 Railscasts Episode.

如何通过Rails创建管理子域,实现子域的集中管理?

它可能看起来像这样.

# in routes.rb map.manage_companies '', :controller => 'companies', :action => 'index', :conditions => { :subdomain => "admin" }

这需要在路由列表中足够高,以便在它之前没有其他匹配.

本文共计559个文字,预计阅读时间需要3分钟。

如何通过Rails创建管理子域,实现子域的集中管理?

我使用的AuthLogic和本文中描述的子域方法运行良好,符合预期。我想探讨的是如何创建一个类似admin或host这样的子域,它将使用AuthLogic进行身份验证的用户。

我使用的是AuthLogic和 this blog post中涵盖的子域方法,一切都运行良好,并且符合预期.我想弄清楚的是如何创建一个像“admin”或“host”这样的子域,它将使用AuthLogic进行身份验证的用户(这可能是微不足道的,无需提及)来管理子域.基本上,所有子域都将正常运行,除了admin.site.com,它将转到自己的控制器和布局.

dhh建议只是引入一个例外来重定向,但是我不确定那里发生了什么,对我来说这似乎不是很简单,任何想法?

编辑
我认为我使用AuthLogic的事实在这里非常重要,因为子域逻辑不会在任何地方转发用户,一旦经过身份验证,AuthLogic会将用户发送到/ account – 所以我的问题可能与如何告知AuthLogic不同的地点有关user是root用户,登录admin子域.

这是我们迄今为止实现的代码

Company Model

class Company < ActiveRecord::Base has_many :users has_many :brands, :dependent => :destroy validates_presence_of :name, :phone, :subdomain validates_format_of :subdomain, :with => /^[A-Za-z0-9-]+$/, :message => 'The subdomain can only contain alphanumeric characters and dashes.', :allow_blank => true validates_uniqueness_of :subdomain, :case_sensitive => false validates_exclusion_of :format, :in => %w( support blog billing help api www host admin manage ryan jeff allie), :message => "Subdomain {{value}} is not allowed." before_validation :downcase_subdomain protected def downcase_subdomain self.subdomain.downcase! if attribute_present?("subdomain") end end

SubdomainCompanies Module

module SubdomainCompanies def self.included( controller ) controller.helper_method(:company_domain, :company_subdomain, :company_url, :company_account, :default_company_subdomain, :default_company_url) end protected # TODO: need to handle www as well def default_company_subdomain '' end def company_url( company_subdomain = default_company_subdomain, use_ssl = request.ssl? ) " : "") end end

Application Controller

class ApplicationController < ActionController::Base include SubdomainCompanies rescue_from 'Acl9::AccessDenied', :with => :access_denied helper :all # include all helpers, all the time protect_from_forgery # See ActionController::RequestForgeryProtection for details helper_method :current_user_session, :current_user, :current_company_name filter_parameter_logging :password, :password_confirmation before_filter :check_company_status protected def public_site? company_subdomain == default_company_subdomain end def current_layout_name public_site? ? 'public' : 'login' end def check_company_status unless company_subdomain == default_company_subdomain # TODO: this is where we could check to see if the account is active as well (paid, etc...) redirect_to default_company_url if current_company.nil? end end end 查看 subdomain-fu,它允许您根据子域路由到不同的控制器和操作.关于这个问题我做了 Railscasts Episode.

如何通过Rails创建管理子域,实现子域的集中管理?

它可能看起来像这样.

# in routes.rb map.manage_companies '', :controller => 'companies', :action => 'index', :conditions => { :subdomain => "admin" }

这需要在路由列表中足够高,以便在它之前没有其他匹配.