How to Use Subdomains and Custom Domains in Rails while in Development Mode

Feature thumb snip20170622 36

Going through this process will allow you to use subdomains and bypass the requirement to use localhost:3000 as your domain in development mode in Rails.

The process for installing on a Mac is as follows:

Install Pow

You can run this command or view the instructions from the website.

curl get.pow.cx | sh

 

Install the Powder Gem

This RubyGem automatically creates a symlink to pow for your app.

gem install powder

 

This will install the gem, after the gem has been installed, change into your application's root directory and run the command:

powder link

 

If everything is properly installed, the output should say:

Your application is now available at http://<your-app-name>.dev/

 

Guide update from @jasonmclean, by linking the domain the server will automatically run, you don't have the need to run rails s, you can access your app from the URL shown above. If you need to restart the server, run the command touch tmp/restart.txt

 

Working with Subdomains

With the new development link working, now you can use subdomains in your application. To test this out, let's generate a controller:

rails g controller Static home

 

Now open up the home.html.erb template and add the code:

<%= request.subdomain %>

 

Now start the rails server again and navigate to http://my-subdomain.<your-app-name>.dev/static/home and you should see whatever the subdomain you chose on the page. From this point you can access the value of the subdomain the same way that you'd use any parameter.

Linux

If you want to implement the same type of functionality on a Linux based system, you can leverage Prax.