【发布时间】:2014-08-19 16:54:28
【问题描述】:
我正在尝试在本地为 Rails 应用程序使用子域,因此我将以下行添加到我的 /etc/hosts 文件中:
# add 'test' subdomain for localhost
127.0.0.1 test.localhost
现在我可以将我的浏览器指向test.localhost:3000,它会点击我的 Rails 应用程序。
但是,Rails 或 WEBrick 将整个 darn thang 解释为域:
# logging in the controller
logger.debug("domain: '#{request.domain}', subdomain: '#{request.subdomain}'")
# output in the console
domain: 'test.localhost', subdomain: ''
有没有简单的方法让 WEBrick Rails 将 test 解释为子域?
谢谢!
更新
我最终制作了一个before_action 作为解决方法。
def set_domain_and_subdomain
@domain = request.domain
@subdomain = request.subdomain
# HACK: force /etc/hosts subdomains
if Rails.env.development?
if m = request.domain.match(/([^\.]+).localhost/)
@subdomain = m[1]
@domain = 'localhost'
end
end
end
但是我仍然很好奇是否有一种方法可以在我的计算机上通用地执行此操作(即在 `/etc/hosts 或其他东西中)
【问题讨论】:
-
我知道像 lvh.me 这样的服务,但我不知道如何让它与 localhost 共享相同的会话,所以它迫使我每次都在 Rails 应用程序中进行 facebook auth :(
标签: ruby-on-rails subdomain webrick