Skip to content

Instantly share code, notes, and snippets.

Last active August 29, 2015 14:18
Show Gist options
  • Save brianburridge/81bd2e4f85d70c0f0a55 to your computer and use it in GitHub Desktop.
Save brianburridge/81bd2e4f85d70c0f0a55 to your computer and use it in GitHub Desktop.
Building Dynamic Apps with Ruby on Rails: Contact Manager

Creating a Contact Manager with Ruby on Rails

Generate an "empty" Rails app with the default stack

rails new contact_manager

Create a model

Next, we need to create a model to represent a Contact. This will allow us to easily gather the information for a Contact, persist it to a database, and retrieve it as needed.

rails generate scaffold Contact name:string phone:string description:string email:string

This uses a generator to create a scaffold. This is not something experienced developers do that often, but it's a great example of how quickly functionality can be added to a Rails application, and, is a great way to get familiar with building a Rails app in the beginning.

Notice the code it generated for us. All of which is now available for us to customize. It created the model, the controller, a migration to modify the database to provide a place to store our contacts, and all the views we need to show the contacts and ask the user for contact information.

Run the migration

Now that we have a migration, which is Rails code to tell the database how to create a table called contacts where we can store our contacts, we have to run the migration. We can do so like this:

rake db:migrate

The database has now been modified according to the migration.

Start the server

Now we need to start the server so we can use our application. The command for that is:

rails s

This starts up a server and runs our app and makes it available for access in our browser at localhost:3000.

When we visit that, we see a default page telling us our Rails app is working. We have to edit our routes to tell Rails where we want it to go when user's go to the home page. The routes.rb file insructs Rails how to handle requests made to our web application.

I'm going to add this route to our routes.rb file:

root 'contacts#index'

Now I can refresh and it takes us to the scaffold that was created to allow us to enter contact data.

Deploying to Heroku

Assuming I've installed the Heroku toolbelt, I can now deploy my app out to Heroku in just a few steps.

First, I need to create a Heroku app:

heroku create

Next, I need to push my code, from my git repository out to Heroku. To do this, I must first setup a local git repository, if I haven't yet done so.

git init .
git add .
git commit -am "First commit"

This will initialize a git repository, add all your oustanding changes to it (all your files in this case), and commit them. Now we are readying to push that code out to Heroku.

git push heroku master

This will take a minute or two, and will setup almost everything we need on Heroku to make our app work. Since we have migrations, in our case to create the Contacts table, we have to remember to run the migrations out on the Heroku server, and then restart the server.

heroku run rake db:migrate
heroku restart

We should now be able to open that app in our browser and use it. The heroku toolbelt gives us a command to open it for us.

heroku open

That's it. Our Contact Manager is now running out on Heroku.

In this short tutorial, we've created a very basic Rails application, checked it into a git repository, and deployed it to a server.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment