Request.subdomain does not install in Rails 3 during conditional routing

I am trying to set up subdomains in Rails 3 for Ryan Bates screencast on subdomains . However, this does not work for me. I have the following setup:

# routes.rb
constraints(Subdomain) do
  get 'devices' => 'devices#all'
end

# lib/subdomain.rb
class Subdomain
  def self.matches?(request)
    # binding.pry
    request.subdomain.present? && request.subdomain == "admin"
  end
end

Downloading the url admin.localhost:3000/devicesshould redirect me to devices#all, however I get a routing error. Routing Error No route matches [GET] "/devices"Clearly my routing is not working.

I am setting up a debugging session prywhere the comment is above and it hits, so my restriction works, but I get the following output:

[1] pry(Subdomain)> request.subdomain
=> ""
[2] pry(Subdomain)> request.subdomains
=> []
[3] pry(Subdomain)> request.host
=> "admin.localhost"

Thus, the rails do not collect the administrative part of the URL and do not put it in the subdomain variable. Although I could just simply use the host value to filter admin routes, I would like to keep it clean and correct.

?


jdoe coreyward . , , , pow xip.io . , http://admin.app_name.192.168.1.68.xip.io/devices tld > 1. , , xip admin.app_name.192.168.1.68, .

+5
2

, Rails .

Rails , (TLD) TLD 1 . , bbc.co.uk, "bbc" , , TLD 2, :

request.subdomain(2) #=> ""

subdomains : . 0 TLD, "admin", , .

, admin.yourapp.dev, localhost, . /etc/hosts, .

, Rails subdomain.

+9

: http://admin.lvh.me:3000/devices

:

config.action_dispatch.tld_length = 0

development.rb .

+5

All Articles