Rails 路由在单个应用程序上处理多个域 [英] Rails routing to handle multiple domains on single application

查看:21
本文介绍了Rails 路由在单个应用程序上处理多个域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管这里和其他地方有几个类似的问题,但我一直无法找到解决这个问题的可行方法.Rails 3 似乎没有回答这个问题,所以这里是:

I've been unable to find a workable solution to this problem, despite several similar questions here and elsewhere. It seems likely that this question hasn't been answered for Rails 3, so here goes:

我有一个应用程序,目前允许用户创建自己的子域,其中包含他们的应用程序实例.虽然在 Rails 2 中最好使用 subdomain-fu gem,但在版本 3 中,它非常简单,根据 Railscast -- http://railscasts.com/episodes/221-subdomains-in-rails-3.

I have an application that currently allows users to create their own subdomain that contains their instance of the application. While in Rails 2 you were best served using the subdomain-fu gem, in version 3 it's dramatically simpler, as per the Railscast -- http://railscasts.com/episodes/221-subdomains-in-rails-3.

这很好,但我还想为用户提供将他们自己的域名与他们的帐户相关联的选项.因此,虽然他们可能拥有 http://userx.mydomain.com,但我希望他们选择拥有 <关联的 href="http://userx.com" rel="noreferrer">http://userx.com.

That's good stuff, but I also want to provide the option for users to associate their own domain name with their account. So while they might have http://userx.mydomain.com, I'd like them to choose to have http://userx.com associated as well.

我在 Rails 2 中找到了一些这样做的参考,但这些技术似乎不再起作用(尤其是这个:https://feefighters.com/blog/hosting-multiple-domains-from-a-single-rails-app/).

I found a few references to doing this in Rails 2, but those techniques don't appear to work anymore (particularly this one: https://feefighters.com/blog/hosting-multiple-domains-from-a-single-rails-app/).

谁能推荐一种使用路由来接受任意域并将其传递给控制器​​以便显示适当内容的方法?

Can anyone recommend a way to use routes to accept an arbitrary domain and pass it along to a controller so I can show the appropriate content?

更新:由于 Leonid 的及时响应以及对代码的全新审视,我现在已经得到了大部分答案.它最终需要添加到我正在使用的现有子域代码(来自 Railscast 解决方案),然后在 routes.rb 中添加一些内容.我还没有完全到达那里,但我想发布到目前为止的内容.

Update: I've gotten most of an answer now, thanks to Leonid's timely response, and a fresh look at the code. It ultimately required an addition to the existing Subdomain code that I was using (from the Railscast solution) and then adding a bit to routes.rb. I'm not all the way there yet but I want to post what I have so far.

在 lib/subdomain.rb 中:

In lib/subdomain.rb:

class Subdomain
  def self.matches?(request)
    request.subdomain.present? && request.subdomain != "www"
  end
end

class Domain
  def self.matches?(request)
    request.domain.present? && request.domain != "mydomain.com"
  end
end

我添加了第二个类来模仿第一个类,这是已知的工作.我只是添加了一个条件,以确保传入的域不是我为其托管主站点的域.

I've added the second class in imitation of the first, which is known working. I simply add a condition that ensures that the incoming domain is not the one for which I'm hosting the main site.

该类在routes.rb中使用:

This class is used in routes.rb:

require 'subdomain'
constraints(Domain) do
  match '/' => 'blogs#show'
end

constraints(Subdomain) do
  match '/' => 'blogs#show'
end

在这里,我在现有的子域代码(同样,它工作正常)前面加上一个节来检查域.如果此服务器响应该域并且它不是主站点运行的域,则转发到指定的控制器.

Here, I'm prepending the existing subdomain code (again, it's working fine) with a stanza to check for the Domain. If this server responds to that domain and it's not the one under which the main site operates, forward to the specified controller.

虽然这似乎奏效了,但我还没有完全完成整个工作,但我认为这个特殊问题已经解决了.

And while that appears to be working, I don't quite have the whole thing working yet, but I think this particular problem has been solved.

推荐答案

根据 ,它实际上在 Rails 3 中更简单http://guides.rubyonrails.org/routing.html#advanced-constraints:

1) 在lib/domain_constraint.rb中定义一个自定义约束类:

1) define a custom constraint class in lib/domain_constraint.rb:

class DomainConstraint
  def initialize(domain)
    @domains = [domain].flatten
  end

  def matches?(request)
    @domains.include? request.domain
  end
end

2) 在路由中使用新的块语法中的类

2) use the class in your routes with the new block syntax

constraints DomainConstraint.new('mydomain.com') do
  root :to => 'mydomain#index'
end

root :to => 'main#index'

或老式的选项语法

root :to => 'mydomain#index', :constraints => DomainConstraint.new('mydomain.com')

这篇关于Rails 路由在单个应用程序上处理多个域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆