Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8094009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:39:23+00:00 2026-06-05T20:39:23+00:00

I’m trying to setup Subdomains in Rails 3 per Ryan Bates screencast on subdomains

  • 0

I’m trying to setup Subdomains in Rails 3 per Ryan Bates screencast on subdomains. However it’s not working 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

Loading the url admin.localhost:3000/devices should route me to devices#all, however I get a routing error Routing Error No route matches [GET] "/devices" Clearly my routing isn’t working.

I setup a pry debug session where the comment is above, and it gets hit, so my constraint is working, but I get the following output:

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

So rails isn’t picking up the admin portion of the url and placing it into the subdomain variable. While I could easily just use the host value to filter to the admin routes, I’d like to keep things clean and correct.

How come rails isn’t setting the subdomain value?


Edit

Both jdoe and coreyward are correct with their answers. What was throwing me off was that I was also using pow and xip.io to access the site and getting the same error. Which was odd because http://admin.app_name.192.168.1.68.xip.io/devices has a tld > 1. What was happening was that the subdomain for xip was admin.app_name.192.168.1.68, which also fails given the matches logic and doesn’t route.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T20:39:25+00:00Added an answer on June 5, 2026 at 8:39 pm

    Because there isn’t a subdomain as far as Rails considers it.

    Rails expects you to provide a Top-Level Domain (TLD) length for TLDs with more than 1 dot. For example, if you had bbc.co.uk it would pickup “bbc” as the subdomain unless you specified the TLD length is 2, in which case the subdomain would be blank:

    request.subdomain(2) #=> ""
    

    This is also why you’re seeing subdomains return an empty array: the last two segments are being discarded automatically. You could specify 0 as the TLD length to get back “admin”, but that code will break in production with a real domain name, so I don’t recommend it.

    I recommend using a domain like admin.yourapp.dev that is routed back to your computer via localhost to make your app properly detect a subdomain. Just edit /etc/hosts to do it simply.

    If you’re curious, here is the Rails source for the subdomain method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.