Implementing Devise in a New Rails App

Devise is wildly powerful but pretty overwhelming the first couple times you use it. I’m going to try and fuddle my way through that process on a new project and document it all here. This guide will start right after generating the new rails project. I’m going to go as slow as possible.

1. add Devise to your gem file.
Screen Shot 2015-04-14 at 5.59.28 PM

I’m using Postgres for my database instead of SQLite. It’s a little easier to deploy to live. That’s the ‘pg’ gem. If you’re starting a fresh project, generate the project with this code to make your life easy: “rails new myapp – -database=postgresql”

If you’re like me and always forget to do that, remove the sqlite gem, add the pg gem, and use this code for you “config/database.yml” file: Railscasts #342

2. Run “bundle install” in your terminal

3. Run “rails generate devise:install” in your terminal
This adds the devise.rb to your config directory among other things. That file has a ton of options that you may need to implement as you use Devise. If this command hangs, you probably have a problem with Spring running in another rails application. Killing that process should fix that.

4. Run “rails generate devise user”
You’ll now have a migration available that creates a user table with email and password fields. Don’t mess with this file. You can always add columns to that table later but the beauty of devise is largely dependent on this migration.

If you’re running Postgres:
4a. Start the Postgres program
You can get it here Postgress App .
4b. Run “rake db:create”

5. Run “rake db:migrate”
Create that Users table!

Optional but recommended:
6. Run “rails generate devise:views users”
This makes a ton of stuff. It creates log in/log out pages, registration pages, and edit user pages. You can edit or delete these as you’d like.

That’s it! You now have a very secure user account system that has easy integration with OmniAuth for other log in systems.

Thanks,
superandrew

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s