Search code examples
ruby-on-railsrubylocalhostsubdomain

rails subdomain on localhost


I would like to test subdomains on localhost. I have following instructions: Before starting the server to run the application, the host should be configured so that the application can support subdomains for multiple users. To do that, go to your hosts file by typing sudo nano /etc/hosts in your terminal, then add subdomains at the end of the file as below:

127.0.0.1 admin.daycare.no
127.0.0.1 daycare.no
127.0.0.1 worker.daycare.no
127.0.0.1 manager.daycare.no
127.0.0.1 parent.daycare.no

I followed the above instructions. The following error was encountered while trying to retrieve the URL: http://daycare.no:3000/

Unable to determine IP address from hostname daycare.no
The DNS server returned: Name Error: The domain name does not exist.
This means that the cache was not able to resolve the hostname presented in the URL. 
Check if the address is correct.

How can I solve this, please?


Solution

  • After saving /etc/hosts file, run your rails app like this

    rails s -p 3000 -b daycare.no
    

    In a browser

     http://daycare.no:3000
    

    Personally, I use lvh.me:3000 just running

    rails s -p 3000 -b lvh.me
    

    no need to touch /etc/hosts file.

    By default, you can't browse the link lvh.me:3000 without internet connection, solution is to add 127.0.0.1 lvh.me into host file.

    # /etc/hosts file
    127.0.0.1   localhost
    127.0.0.1   lvh.me #make sure lvh.me is after localhost otherwise will not work
    

    But, Its little annoying to run this each time when restarting a server.

    Set your custom command:

    sudo nano .bash_profile
    # OR
    sudo nano .bashrc
    # OR
    sudo nano .profile
    

    And ADD these lines to there:

    alias lvh='rails s -p 3000 -b lvh.me'
    alias lvh_production='RAILS_ENV=production rails s -p 3000 -b lvh.me' #production
    

    Do not forget restart your terminal tab, close and open new tab OR run this command on the same tab . ~/.bash_profile depends what you have used on top.


    Alternate solution is POW (LINK) server can give you custom domain smth like daycare.dev.