Rails 3.x TLD长度

8

在Rails的配置中是否有可以全局设置TLD长度为2(以co.uk为例),以便于request.domain和request.subdomain正确解析而无需传递选项?

也就是说,request.domain(2),默认情况下Rails似乎已经设置为1,并且更改此全局设置是有意义的,但是,在文档中没有找到这样的信息。

是否存在这样的配置选项?

3个回答

11

在您的 config/environments/production.rb 文件中添加以下行:

config.action_dispatch.tld_length = 2

config.action_dispatch.tld_length设置应用程序的顶级域名(TLD)长度。默认值为1。

http://guides.rubyonrails.org/configuring.html


8
在Rails 3.1中,您可以设置:
ActionDispatch::Http::URL.tld_length = 2

谢谢,这对我以后转移到3.1生产环境中非常有帮助。 - user427165
2
我看到有一个config.action_dispatch.tld_length,它设置了ActionDispatch::Http::URL.tld_length。我会查看3.1版本,看看helpers是否使用它。 - user427165

2

对于Rails 3.0.9及以下版本,没有这样的配置,因为domain的源代码如下:

# File actionpack/lib/action_dispatch/http/url.rb, line 78
def domain(tld_length = 1)
  return nil unless named_host?(host)

  host.split('.').last(1 + tld_length).join('.')
end

来源:http://apidock.com/rails/v3.0.9/ActionDispatch/Http/URL/domain

该网址介绍了Rails中ActionDispatch::Http::URL模块的domain方法,可用于获取URL主机名(即域名)。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接