Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/52.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何创建管理子域来管理Rails中的子域_Ruby On Rails_Subdomain - Fatal编程技术网

Ruby on rails 如何创建管理子域来管理Rails中的子域

Ruby on rails 如何创建管理子域来管理Rails中的子域,ruby-on-rails,subdomain,Ruby On Rails,Subdomain,我正在使用AuthLogic和dhh所涵盖的子域方法,一切都很好,正如预期的那样。我想弄明白的是如何创建一个子域,比如“admin”或“host”,它将让用户通过AuthLogic的身份验证(这可能很简单,不需要提及),从而管理子域。因此,基本上,所有子域都将正常运行,除了admin.site.com,它将进入自己的控制器和布局 dhh建议只是抛出一个异常来重定向,但我不确定这会发生什么,对我来说似乎没那么简单,有什么想法吗 编辑 我认为我在这里使用AuthLogic这一事实很重要,因为子域逻辑

我正在使用AuthLogic和dhh所涵盖的子域方法,一切都很好,正如预期的那样。我想弄明白的是如何创建一个子域,比如“admin”或“host”,它将让用户通过AuthLogic的身份验证(这可能很简单,不需要提及),从而管理子域。因此,基本上,所有子域都将正常运行,除了admin.site.com,它将进入自己的控制器和布局

dhh建议只是抛出一个异常来重定向,但我不确定这会发生什么,对我来说似乎没那么简单,有什么想法吗

编辑 我认为我在这里使用AuthLogic这一事实很重要,因为子域逻辑不会将用户转发到任何地方,一旦经过身份验证的AuthLogic将用户发送到/account,那么我的问题可能是,如果用户是根用户,登录到管理子域,我如何将AuthLogic告知其他位置

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

公司模式

class公司:破坏
验证是否存在:name、:phone、:子域
验证以下内容的格式::子域,:with=>/^[A-Za-z0-9-]+$/,:message=>'子域只能包含字母数字字符和破折号',:allow_blank=>true
验证:子域的唯一性,区分大小写=>false
验证以下内容的排除::format,:in=>%w(支持博客计费帮助api www host admin manage ryan jeff allie),:message=>“不允许使用子域{value}。”
验证前:downcase\u子域
受保护的
def downcase_子域
self.subdomain.downcase!如果属性_存在?(“子域”)
结束
结束
子域公司模块

模块子域公司
def自带(控制器)
controller.helper\u方法(:company\u domain,:company\u subdomain,:company\u url,:company\u account,:default\u company\u subdomain,:default\u company\u url)
结束
受保护的
#TODO:也需要处理www
def default_company_子域
''
结束
def company_url(company_子域=默认_company_子域,使用_ssl=request.ssl?)
http_协议(使用_ssl)+公司_主机(公司_子域)
结束
def公司_主机(子域)
公司\主机=“”
查看允许您根据子域路由到不同控制器和操作的公司。关于这个问题,我做了大量的研究

它可能看起来像这样

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

这需要在路由列表中占据足够高的位置,以便在它之前没有其他匹配项。

对于RAILS 2.3:您可以下载一个完整的示例应用程序(附带一个分步教程),展示如何实现管理子域,一个主域,以及多个用户子域,所述多个用户子域使用所述设计gem用于认证,所述子域路由gem用于管理子域。以下是链接:


对于RAILS 3:以下是完整的示例实现(以及详细的教程)。在Rails 3中实现这一点比在Rails 2中要容易得多(不需要插件)。

我真的想避免使用插件,我觉得对于我们的简单需求来说,这太过分了。但是,我可能不得不这样做——如果用户点击了该url,并且他们没有经过身份验证,子域fu会设法重定向他们或显示正确的布局吗?在我去安装插件之前,我想确保它能与Authlogic一起工作。。你这样做了吗?
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? )
      http_protocol(use_ssl) + company_host(company_subdomain)
    end

    def company_host( subdomain )
      company_host = ''
      company_host << subdomain + '.'
      company_host << company_domain
    end

    def company_domain
      company_domain = ''
      company_domain << request.domain + request.port_string
    end

    def company_subdomain
      request.subdomains.first || ''
    end

    def default_company_url( use_ssl = request.ssl? )
      http_protocol(use_ssl) + company_domain
    end      

    def current_company
      Company.find_by_subdomain(company_subdomain)
    end

    def http_protocol( use_ssl = request.ssl? )
      (use_ssl ? "https://" : "http://")
    end 
end
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
# in routes.rb
map.manage_companies '', :controller => 'companies', :action => 'index', :conditions => { :subdomain => "admin" }