Rails 3.x TLD length
Asked Answered
Z

3

8

Is there some where in Rails's configuration where I can globally set the TLD length to 2 (co.uk as an example) so request.domain and request.subdomain parse correctly without having to pass options?

That is, request.domain(2), by default Rails seems to be set to 1 by default and it makes sense to be able to change this globally, however, haven't been able to find anything in the documentation.

Does such a configuration option exist?

Zeldazelde answered 16/8, 2011 at 14:15 Comment(0)
A
2

For Rails 3.0.9 and below, there's no such configuration since the source of domain is:

# 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

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

Acrostic answered 16/8, 2011 at 14:21 Comment(0)
W
11

In your config/environments/production.rb file add this line:

config.action_dispatch.tld_length = 2

config.action_dispatch.tld_length sets the TLD (top-level domain) length for the application. Defaults to 1.

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

Weidman answered 12/4, 2013 at 18:41 Comment(0)
C
8

In Rails 3.1 you can set:

ActionDispatch::Http::URL.tld_length = 2
Candelaria answered 16/8, 2011 at 14:25 Comment(2)
Thanks that's really good to know for when I move to 3.1 for production.Zeldazelde
I see there is a config.action_dispatch.tld_length which sets ActionDispatch::Http::URL.tld_length I'll look at 3.1 to see if helpers use it.Zeldazelde
A
2

For Rails 3.0.9 and below, there's no such configuration since the source of domain is:

# 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

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

Acrostic answered 16/8, 2011 at 14:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.