Rails 4 应用程序中的子域 [英] Subdomains in a Rails 4 app

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

问题描述

今天我遇到了一个相当奇怪的现象.在开发每个用户都有自己的子域的 Rails 应用程序,并尝试使用 Devise 来实现时,我遇到了未注册的子域也会路由到根页面的情况.因此,例如,即使没有(显式)子域,它也会将我路由到主应用程序页面.也许这与我的特定设置有关?我还尝试了一个新的 Rails 项目,但得到了相同的结果.任何人都可以为我澄清这一点?Railscasts 没有成功.

Today I came across a rather weird phenomenon. When developing a Rails app in which every user has his own subdomain, and trying to use Devise to do it, I encountered that also subdomains not registered whatsoever would route to the root page. So, for example, even if there was no (explicit) subdomain, it would route me to the main application page. Maybe this has to do with my particular setup? I also tried with a new Rails project, but I got the same result. Anyone able to clarify this for me? Railscasts didn't do the trick.

另外,现在我正在使用 WEBrick,尽管我不会在生产中使用它,而且我正在使用域 lvh.me 来访问子域.

Also, right now I'm using WEBrick, even though that's not what I'll be using in production, and I'm using the domain lvh.me to access the subdomains.

感谢您的帮助.

这是我的 routes.rb 文件:

Here's my routes.rb file:

Rails.application.routes.draw do
    devise_for :users
    root 'static_page#index'
end

我遵循了 Devise wiki 中的本指南:https://github.com/plataformatec/devise/wiki/How-to:-Scope-login-to-subdomain

I followed this guide from the Devise wiki: https://github.com/plataformatec/devise/wiki/How-to:-Scope-login-to-subdomain

所以我所做的是通过迁移删除了电子邮件的唯一性,然后我将 :subdomain 添加到 request_keys 如下:

So what I did was I removed the uniqueness of the email through a migration, and next I added :subdomain to request_keys as follows:

devise :database_authenticatable, :recoverable, :rememberable, :trackable, :timeoutable, request_keys: [:subdomain]

然后我继续重写find_for_authentication"函数,如下所示:

Then I went on to override the "find_for_authentication" function, as per the following:

def self.find_for_authentication(conditions={})
    conditions[:account_id] = Account.find_by_subdomain(conditions.delete(:subdomain)).id
    super(conditions)
end

这就是总结.

我一直在胡闹,我找到了问题所在.root"指令将所有子域都指向我的域.因此,如果我从 routes.rb 中删除根,每个 子域都会导致 RouteError.我记得在我的应用程序中的某个地方需要一个用于 Devise 的根.所以我不确定这是 Devise 的行为还是 root 的行为.

I've been messing around, and I've found the problem. The 'root' directive refers all subdomains to my domain. So if I remove the root from my routes.rb, every subdomain leads to a RouteError. I remember about needing a root somewhere in my app for Devise. So I'm not sure if this is Devise's behaviour or root's.

推荐答案

我手动解决了.我发现 routes.rb 中的root"指令接受来自每个子域的请求.最后,我实现了一个自定义约束,并将其添加到 routes.rb 文件中 devise_for 的约束中.这充分解决了我的问题.谢谢大家的帮助!

I solved it manually. I found out the 'root' directive in the routes.rb accepts requests from every subdomain. In the end, I implemented a custom constraint and added this to the constraint for the devise_for in the routes.rb file. This adequately solved my problem. Thank you all for your help!

更新:

为了回应要求我的代码的评论:我们设置了一个系统,每个帐户(企业帐户)都有自己的子域,每个企业帐户都有许多用户.我的子域约束看起来像

In response to the comment asking for my code: we had a system set up whereby each account (business account) would have their own subdomain, and each business account would have many users. My subdomain constraint looks like

lib/subdomain.rb

class Subdomain
  def self.matches?(request)
    request.subdomain.present? && Account.exists?(subdomain: request.subdomain)
  end
end

然后在我的 config/routes.rb 中,我使用该约束来路由请求:

Then in my config/routes.rb I used that constrain to route the requests:

config/routes.rb

require 'Subdomain'

Rails.application.routes.draw do
  # Some routes...
  constraints(Subdomain) do
    devise_for :users, controllers: {:sessions => 'session'}
    root 'dashboard#index'
    # More subdomain constrained routes...
  end
end

在我发布的原始问题中,我已经展示了我的其余设置.我希望这对某人有所帮助.

In my original question I'd posted I'd already shown the rest of my setup. I hope this helps someone.

这篇关于Rails 4 应用程序中的子域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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