Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 8540787cb6
Fetching contributors…

Cannot retrieve contributors at this time

273 lines (158 sloc) 15.118 kb

Rails Application for Subdomains with Devise

Rails 3.2 example application shows how to use subdomains with Devise for authentication.

The Devise gem gives you ready-made authentication and user management. MongoDB is used as a datastore with the Mongoid gem for quick development without schemas or migrations.

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 a GitHub issue.

Follow on Twitter Follow on Twitter

Follow the project on Twitter: @rails_apps. Please tweet some praise if you like what you’ve found.

Tutorial Tutorial

A complete walkthrough tutorial is available on the GitHub wiki:

View the Tutorial

The tutorial documents each step to follow to create the 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. See a list of recommended resources for Rails.

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 — and What Is Not

The example app implements a common use of subdomains, often called “Basecamp-style subdomains in Rails.” Visitors to the main site create a user account which is 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.

Another common use of subdomains can be called “blog-style subdomains.” This approach is familiar to users of sites such as wordpress.com where each user can create multiple blogs and each is hosted on its own subdomain. For example, a user with the email address “user@example.com@” can own more than one blog, for example, “puppyphotos.wordpress.com” and “kittenphotos.wordpress.com”. The “blog-style subdomains” approach is not implemented in this example application.

Similar Examples and Tutorials

See a list of additional Rails examples, tutorials, and starter apps.

Dependencies

Before running this app, you will need:

  • The Ruby language (version 1.9.3)
  • Rails 3.2
  • A working installation of MongoDB (version 1.6.0 or newer)

See Installing Rails 3.2 for detailed instructions and advice.

Installing MongoDB

If you don’t have MongoDB installed on your computer, you’ll need to install it and set it up to be always running on your computer (run at launch). On Mac OS X, the easiest way to install MongoDB is to install Homebrew and then run the following:

brew install mongodb

Homebrew will provide post-installation instructions to get MongoDB running. The last line of the installation output shows you the MongoDB install location (for example, /usr/local/Cellar/mongodb/1.8.0-x86_64). You’ll find the MongoDB configuration file there. After an installation using Homebrew, the default data directory will be /usr/local/var/mongodb.

Getting the Application

You have several options for getting the code.

Downloading the Code

If you simply wish to examine the example code, you can download the code (“clone the repository”) with the command

$ git clone git://github.com/RailsApps/rails3-subdomains.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/).

Using the Ready-Made Application Template

You can use an application template to generate a new version of the example app. You’ll find an application template for this app in the Rails Application Templates repository.

Use the command:

$ rails new myapp -m https://github.com/RailsApps/rails3-application-templates/raw/master/rails3-subdomains-template.rb -T -O

Use the -T -O flags to skip Test::Unit files and Active Record files.

This creates a new Rails app (with the name myapp) on your computer.

The application generator templates will ask you for various preferences:

  • Would you like to use Haml instead of ERB?
  • Would you like to use RSpec instead of TestUnit?
  • Would you like to use factory_girl for test fixtures with RSpec?
  • Would you like to use Cucumber for your BDD?
  • Would you like to use Guard to automate your workflow?
  • Would you like to use Mongoid to connect to a MongoDB database?
  • Would you like to use Devise for authentication?
  • Would you like to set a robots.txt file to ban spiders?
  • Would you like to use ‘rails-footnotes’ during development?
  • Would you like to add support for subdomains?
    1. No
    2. One subdomain per user (like Basecamp)
  • Which front-end framework would you like for HTML5 and CSS3?
    1. None
    2. Zurb Foundation
    3. Twitter Bootstrap
    4. Skeleton
    5. Normalize CSS for consistent styling

Use “Recipes” to Customize an Application Template

The tutorial shows how a customized application template can be assembled from “recipes.” The application template was created using the Rails Apps Composer gem which provides a convenient way to assemble a reusable application template by selecting various “recipes” for popular Rails development packages.

Please Remember: Edit the README

If you’re open sourcing the app on GitHub, please edit the README file to add a description of the app and your contact info. Changing the README is important if you’re using a clone of the example app. I’ve been mistaken (and contacted) as the author of apps that are copied from my example.

Getting Started

About Required Gems

The application uses the following gems:

Install the Required Gems

Install the required gems on your computer:

$ bundle install

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 Mongoid

Mongoid provides access to the MongoDB database from Rails.

You can use the default configuration found in the file config/mongoid.yml.

If you want to see what’s in your MongoDB databases, I recommend using the MongoHub app (for Mac OS X).

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:

config/initializers/devise.rb

Configure Email for Devise

Configure email by modifying

config/initializers/devise.rb

and setting the return email address for emails sent from the application.

You may need to set values for your mailhost in

config/environments/development.rb
config/environments/production.rb

Create a Default User

Set Up a Database Seed File

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 'EMPTY THE MONGODB DATABASE'
Mongoid.master.collections.reject { |c| c.name =~ /^system/}.each(&:drop)
puts 'SETTING UP DEFAULT USER LOGIN'
user = User.create! :name => 'myname', :email => 'user@example.com', :password => 'please', :password_confirmation => 'please'
puts 'New user created: ' << user.name

Use the defaults or change the values for name, email, and password as you wish.

Seed the Database

Add the default user to the MongoDB database by running the command:

$ rake db:seed

Test the Application With Subdomains

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).

To test the application, visit http://lvh.me:3000/. You should see a list of registered users with links to their profile pages.

Try http://myname.lvh.me:3000/. You should see the profile page for the default user “myname.”

Try http://foo.lvh.me:3000/. You should see the “user not found” error message.

Try http://www.lvh.me:3000/. You should be redirected to the application home page at http://lvh.me:3000/.

To sign in as the default user, (unless you’ve changed it) use

  • email: user@example.com
  • password: please

You should delete or change the pre-configured logins before you deploy your application.

Deploy to Heroku

For your convenience, here are instructions for deploying your app to Heroku. Heroku provides low cost, easily configured Rails application hosting.

Customizing

Devise provides a variety of features for implementing authentication. See the Devise documentation for options.

This example application and tutorial demonstrates Devise and Mongoid working together on Rails 3. Add any models, controllers, and views that you need.

Testing

The application is set up for RSpec unit tests and Cucumber scenarios and steps. No tests and scenarios are implemented for this version of the example application. Please send the author a message, create an issue, or submit a pull request if you can contribute RSpec or Cucumber tests and scenarios.

After installing the application, run rake -T to check that rake tasks for RSpec and Cucumber are available.

Run rake spec to run all RSpec tests.

Run rake cucumber (or more simply, cucumber) to run all Cucumber scenarios and steps.

Documentation and Support

See the Tutorial for this app for details of how it was built.

For a Mongoid introduction, Ryan Bates offers a Railscast on Mongoid. You can find documentation for Mongoid at http://mongoid.org/ There is an active Mongoid mailing list and you can submit Mongoid issues at GitHub.

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.

Issues

Please create an issue on GitHub if you identify any problems or have suggestions for improvements.

Where to Get Help

Your best source for help with problems is Stack Overflow. Your issue may have been encountered and addressed by others.

You can also try Rails Hotline, a free telephone hotline for Rails help staffed by volunteers.

Contributing

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.

Credits

Thank you to Brian Cardarella for a helpful blog post on Custom Subdomains in Rails 3.

Daniel Kehoe (http://danielkehoe.com/) implemented the application and wrote the tutorial.

Is the app useful to you? Follow the project on Twitter:
@rails_apps
and tweet some praise. I’d love to know you were helped out by what I’ve put together.

License

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.

Jump to Line
Something went wrong with that request. Please try again.