我可以让Rails / WEBrick将/ etc / hosts中的条目识别为子域(而不是域)吗?
我正在尝试在本地使用子域用于Rails应用程序,因此我将以下行添加到我的/etc/hosts
文件中:
# add 'test' subdomain for localhost 127.0.0.1 test.localhost
现在我可以将浏览器指向test.localhost:3000
并点击我的Rails应用程序。
但是,Rails或WEBrick将整个域名解释为域:
# 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或其他东西)
很晚才发现这篇文章,但对后人来说: https : //github.com/rails/rails/issues/12438
设置顶级域名长度(TLD)允许request.subdomain
按照您的预期定位子域。
我把config.action_dispatch.tld_length = 0
放到config/environments/development.rb
,一切都顺利进行。
请记住重新启动服务器