This is an example Rails 3 application that shows how to use Devise with subdomains. The Devise gem gives you ready-made authentication and user management.
Best of all, there’s a detailed tutorial (walk-through) to show how it’s built.
You can clone this app or generate a new Rails application using this app as a template.
Any issues? Please create an Issue on GitHub.
To keep up to date, follow the project on Twitter:
Please tweet some praise if you like what you’ve found.
“Building It” Tutorial
A complete walkthrough tutorial is available on the GitHub wiki:
The tutorial documents each step that you must follow to create this application. Every step is documented concisely, so a complete beginner can create this application without any additional knowledge. However, no explanation is offered for any of the steps, so if you are a beginner, you’re advised to look for an introduction to Rails elsewhere.
If you simply wish to modify the application for your own project, you can download the application and set it up as described below, without following the tutorial.
What Is Implemented
This example implements “blog-style subdomains in Rails.” The example is similar to the application shown in Ryan Bates’s screencast Subdomains in Rails 3 but adds authentication using Devise. In this example, there is a “main” domain where anyone can visit and create a user account. And registered users can create any number of subdomains which could host blogs or other types of sites.
What Is Not Implemented
Another use of subdomains is often called “Basecamp-style subdomains in Rails.” Visitors to the main site can create a user account which is then hosted at a subdomain that matches their user name. Each user has only one subdomain and when they log in, all their activity is confined to their subdomain. A user’s home page and account info is accessed only through the subdomain that matches their user name.
For an implementation of “Basecamp-style subdomains in Rails,” see:
Sachin Sagar Rai (millisami) has revised Steve Alex’s Basecamp-style fork to use Mongodb with Mongoid:
(Got your own? Contact me and I will add it here.)
No testing (RSpec or otherwise) is implemented. This app only serves to demonstrate Devise working with subdomains on Rails 3.
Similar Examples and Tutorials
|Plataformatec||Devise||Simple authentication example using SQLite, no subdomains|
|Daniel Kehoe||Devise, RSpec, Cucumber||Detailed tutorial, app template, starter app, using SQLite, no subdomains|
|Daniel Kehoe||OmniAuth, Mongoid||Detailed tutorial, app template, starter app, using MongoDB, no subdomains|
Before running this app, you need to install
- The Ruby language (version 1.8.7 or 1.9.2)
- Rails (version 3.0.4)
- A working installation of SQLite (preferred), MySQL, or PostgreSQL
I recommend installing rvm, the Ruby Version Manager, to manage multiple versions of Rails.
Check that appropriate versions of Ruby and Rails are installed in your development environment:
$ ruby -v
$ rails -v
Generating the Application
To get started with a new Rails application based on this example, you can generate a new Rails app:
$ rails new app_name -m https://github.com/fortuity/rails3-application-templates/raw/master/rails3-subdomain-devise-template.rb
You MUST be using Rails 3.0.4. Generating a Rails application from an “HTTPS” URL does not work in Rails 3.0.3 and earlier versions.
This creates a new Rails app (with the
app_name you provide) on your computer.
The application template offers you the following options:
- set up your view files using the Haml templating language
- use jQuery instead of Prototype
- install the heroku gem for deployment to Heroku
- use Mongoid instead of Active Record for database access
If you wish to “change the recipe” to generate the app with your own customized options, you can copy and edit the file rails3-subdomain-devise-template.rb found at the project fortuity/rails3-application-templates.
Downloading the Example
I recommend “Generating the Application” as described above. If that doesn’t work, or you simply wish to examine the example code, you can download the app (“clone the repository”) with the command
$ git clone git(at)github.com:fortuity/rails3-subdomain-devise.git
The source code is managed with Git (a version control system). You’ll need Git on your machine (install it from http://git-scm.com/).
Set Up Gems
About Required Gems
The application uses the following gems.
The SQLite3 gem is used for the database. You can substitute a different database if you wish.
The FriendlyId gem is used to give users and subdomains easily recognizable strings instead of numeric ids in URLs.
I recommend checking for newer versions of these gems before proceeding:
- rails (version 3.0.4) (check rubygems.org for a newer gem)
- devise (version 1.2.rc) (Check rubygems.org for a newer gem)
- friendly_id (version 3.2.1) (Check rubygems.org for a newer gem)
The app has been tested with the indicated versions. If you are able to build the app with a newer gem, please create an issue on GitHub and I will update the app.
Install the Required Gems
Install the required gems on your computer:
$ bundle install
If you need to troubleshoot, you can check which gems are installed on your computer with:
$ gem list --local
Keep in mind that you have installed these gems locally. When you deploy the app to another server, the same gems (and versions) must be available.
Configure email by modifying
and setting the return email address for emails sent from the application.
You may need to set values for your mailhost in
Set Up Configuration for Devise
This app uses Devise for user management and authentication. Devise is at http://github.com/plataformatec/devise.
You can modify the configuration file for Devise if you want to use something other than the defaults:
Set Up the Database
Create a Database and Run Migrations
Create an empty database. You can do this by running a rake command:
$ rake db:create
Run the migrations:
$ rake db:migrate
You can take a look at the database schema that’s been created for you:
Seed the Database With Users and Subdomains
You’ll want to set up a default user so you can easily log in to test the app. You can modify the file db/seeds.rb for your own name, email and password:
puts 'SETTING UP EXAMPLE USERS' user1 = User.create! :name => 'First User', :email => 'firstname.lastname@example.org', :password => 'please', :password_confirmation => 'please' puts 'New user created: ' << user1.name user2 = User.create! :name => 'Other User', :email => 'email@example.com', :password => 'please', :password_confirmation => 'please' puts 'New user created: ' << user2.name puts 'SETTING UP EXAMPLE SUBDOMAINS' subdomain1 = Subdomain.create! :name => 'foo' puts 'Created subdomain: ' << subdomain1.name subdomain2 = Subdomain.create! :name => 'bar' puts 'Created subdomain: ' << subdomain2.name user1.subdomains << subdomain1 user1.save user2.subdomains << subdomain2 user2.save
Run the rake task to seed the database:
$ rake db:seed
Test the App
You can check that your app runs properly by entering the command
$ rails server (or, abbreviated:
$ rails s)
If you launch the application, it will be running at http://localhost:3000/ or http://0.0.0.0:3000/. However, unless you’ve made some configuration changes to your computer, you won’t be able to resolve an address that uses a subdomain, such as http://foo.localhost:3000/. There are several complex solutions to this problem. You could set up your own domain name server on your localhost and create an A entry to catch all subdomains. You could modify your /etc/hosts file (but it won’t accommodate dynamically created subdomains). You can create a proxy auto-config file and set it up as the proxy in your web browser preferences. There’s a far simpler solution that does not require reconfiguring your computer or web browser preferences. The developer Levi Cook registered a domain, lvh.me (short for: local virtual host me), that resolves to the localhost IP address 127.0.0.1 and supports wildcards (accommodating dynamically created subdomains). See Tim Pope’s blog post for a NSFW alternative.
To sign in as the default user, (unless you’ve changed it) use
- email: firstname.lastname@example.org
- password: please
You should delete or change the pre-configured logins before you deploy your application.
Deploying to Heroku
Set Up Heroku
For your convenience, here are instructions for deploying your app to Heroku. Heroku provides low cost, easily configured Rails application hosting.
To deploy this app to Heroku, you must have a Heroku account. If you need to obtain one, visit http://heroku.com/ to set up an account.
Make sure the Heroku gem is in your Gemfile. If it’s not, add it and run
$ bundle install
to set up your gems again.
If you’ve just created a Heroku account, add your public key immediately after installing the heroku gem so that you can use git to push or clone Heroku app repositories. See http://docs.heroku.com/heroku-command for details.
Create Your Application on Heroku
Use the Heroku create command to create and name your new app.
$ heroku create _myapp_
Heroku Add-ons and DNS Configuration
You will need the following Heroku add-ons to deploy your app using subdomains with your own custom domain:
- Custom Domains (free)
- Custom Domains + Wildcard ($5 per month)
- Zerigo DNS Tier 1 ($7 per month)
To enable the add-ons, you can use the Heroku web interface or you can enter the following commands:
$ heroku addons:add custom_domains
$ heroku domains:add mydomain.com
$ heroku addons:add wildcard_domains
$ heroku domains:add *.mydomain.com
$ heroku addons:add zerigo_dns:tier1
If you are using the Zerigo DNS service, you will need to set the nameserver with your domain registrar. It can take a few minutes (or longer) for DNS changes to propagate. When DNS is set properly, you should be able to visit mydomain.com or test.mydomain.com in your web browser and see the Heroku default page:
Heroku | Welcome to your new app!
You can check that everything has been added correctly by running:
$ heroku info --app myapp
Set Up Your Application on Heroku
Push your application to Heroku:
$ git push heroku master
Set up your Heroku database:
$ heroku rake db:migrate
Initialize your application database:
$ heroku rake db:seed
Visit Your Site
If you use the heroku command to open your default web browser to your site with
$ heroku open
or if you visit your site with http://myapp.heroku.com/ you’ll see the error, “The page you were looking for doesn’t exist.” That’s because your app is trying to find a subdomain “myapp” that doesn’t exist. You’ll need to visit the site using your own domain name, such as http://mydomain.com/. Domain name service must be set properly to use the Zerigo nameservers.
If you get errors, you can troubleshoot by reviewing the log files:
$ heroku logs
You can use the Site model, controller, and views as a beginning point for customizing the app. For example, you could build a blog application that is displayed on the Site pages.
Devise provides a variety of features for implementing authentication. See the Devise documentation for options.
The application does not include tests (RSpec or otherwise). It relies on Devise which include extensive tests. This application is intended to be a basis for your own customized application and (in most cases) you will be writing your own tests for your required behavior.
Documentation and Support
See the Tutorial for this app for details of how it was built.
For a Devise introduction, Ryan Bates offers a Railscast on Devise. You can find documentation for Devise at http://github.com/plataformatec/devise. There is an active Devise mailing list and you can submit Devise issues at GitHub.
Please create an issue on GitHub if you identify any problems or have suggestions for improvements.
If you make improvements to this application, please share with others.
Send the author a message, create an issue, or fork the project and submit a pull request.
If you add functionality to this application, create an alternative implementation, or build an application that is similar, please contact me and I’ll add a note to the README so that others can find your work.
Daniel Kehoe (http://danielkehoe.com/) implemented the application and wrote the tutorial.
Thank you to contributor Fred Schoeneman for improving the tutorial.
Thank you to contributor Charlie Ussery for suggesting how to ignore the “www” subdomain.
Thank you to contributor Tom Howlett for suggesting how to use subdomains in Devise emails.
Thank you to contributor Tom Mornini for improvements to forms.
Public Domain Dedication
This work is a compilation and derivation from other previously released works. With the exception of various included works, which may be restricted by other licenses, the author or authors of this code dedicate any and all copyright interest in this code to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this code under copyright law.