Sometimes you might find yourself wanting to quickly prototype an application that requires user sign ups. Here’s a quick guide to setting up a new rails application with user signup and email confirmation.
Example project available here: https://github.com/rhardih/rails4-with-user-signup. Each step below will be annotated with a commit linked on Github.
Set up a new rails project
- rails new rails4-with-user-signup -d postgresql
- cd rails4-with-user-signup
- bin/rake db:create
- bin/rails s
If you’re on OS X using PostgreSQL, you might see this error intially:
could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket “/var/pgsql_socket/.s.PGSQL.5432”?
One extra step adjusting the config/database.yml is needed. Just uncomment the host option and you should be good to go. If you go to http://localhost:3000, you should see the familiar “Welcome aboard, You’re riding Ruby on Rails!” message page.
Add Devise for user sign up and authentication
- Follow the Getting Started section of the Devise README. Commits be1c60, be34ad, 3625cd.
- Then follow the the Devise wiki page for adding :confirmable to Users. Commits eedaab, 862a3e, 2733d8, 4ee44b.
- Additonally, to make sure you can actually send email in development mode, add the following options to config/environments/development.rb:
config.action_mailer.default_url_options = { host: ‘localhost’, port: 3000 }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {:address => “localhost”, :port => 1025}Commit: 35e177.
Setup and run Mailcatcher to capture Devise sign up emails
- Install: gem install mailcatcher
- Run: mailcatcher
Test run
Open another tab at http://localhost:1080, where you can see the mailcatcher interface with an empty mail queue.
Now go to http://localhost:3000/users/sign_up and create a new user.
Check again in the mailcatcher interface. You should now see an email with the subject “Confirmation instructions”.
Congratulations! You’ve just set up a a new rails application with user sign up, authentication and email confirmation.