January 28, 2021
Hot Topics:

Take Rails to the Cloud: Deploying a Rails Application to Heroku

  • By Mark Watson
  • Send Email »
  • More Articles »

Modifying the Scaffold Views

For this exercise, you will modify the scaffold views only to show a search term input field and search results on any page the user sees after logging in (i.e., any page generated with data from the NotesController).

Start by editing the file app/views/layouts/notes.html.erb by adding:

<div style="float:right;">
    <strong>Search:</strong> <input type="text" name="search" value="<%=params[:search]%>"/>
  <%= @results %>

Figure 1 shows a screenshot of the scaffold web app with an added search field.

Now, add a bit of code to the NotesController class:

  before_filter :do_search # needs to go after "before_filter :require_login"
  def do_search
    @results = ''
    if params[:search]
      notes = Note.search(params[:search])
      @results += "<h4>Results:</h4>" if notes.length>0
      notes.each {|note| @results += "<a href=\"/notes/#{note.id}\">#{note.title}</a><br/>"}

With these simple changes to the notes controller and the default notes layout, every scaffold page becomes search enabled.

Creating the Database on Heroku

You can use another command line tool to define this environment variable in the context of your web application deployed on Heroku. Define a blank password because your Heroku database is available only in your application's runtime context, so a password won't be required to make a database connection:

$ heroku config:add POSTGRESQL_PASSWORD=
Adding config vars:
Restarting app...done.
I can now create my database:
$ heroku rake db:migrate
(in /disk1/home/slugs/86008_449e6c2_e27f/mnt)
==  CreateNotes: migrating ====================================================
-- create_table(:notes)
   -> 0.0141s
==  CreateNotes: migrated (0.0142s) ===========================================
==  CreateUsers: migrating ====================================================
-- create_table(:users)
   -> 0.0112s
==  CreateUsers: migrated (0.0338s) ===========================================

Heroku offers many timesaving features for web application developers. One of them is opening your system's default web browser with the URL of your deployed application:

$ heroku open Opening http://note-taker-pg.heroku.com/

Development Work Flow Using Heroku

When I deploy to one of my own servers or to an Amazon EC2 instance, I do a lot of work using a combination of Capistrano and Chef to set up a custom deployment scheme. Using Heroku eliminates all of this setup work. Although I like to host my git repositories on one of my own servers, you can use the repository created for your web application on Heroku (See "Sidebar 1. Using Git"). As an example, if I wanted to create a new working copy of my application (say I needed to make a change while at a friend's house), I could treat my Heroku git repository as any other remote repository. Here is how I would check out a new copy:

git clone git@heroku.com:note-taker-pg.git

When you check out a new clone of your project, it will not be set up for Heroku. So, you would need to reuse the heroku command line program. This requirement is why I usually just use my original copy for development.

During development, you edit and test against the local copy of your web application. You can at anytime see which files you have modified, commit changes, and redeploy your application on Heroku in a few seconds:

git status
# On branch master
# Changed but not updated:
#   (use "git add ..." to update what will be committed)
#   (use "git checkout -- ..." to discard changes in working directory)
#   modified:   app/controllers/notes_controller.rb
# Untracked files:
#   (use "git add ..." to include in what will be committed)
#   config/database-local.yml
no changes added to commit (use "git add" and/or "git commit -a")
markws-macbook:note_taker_pg markw$ git commit -a -m "added comments to notes controller"
[master 3639dce] added comments to notes controller
 1 files changed, 1 insertions(+), 0 deletions(-)
markws-macbook:note_taker_pg markw$ git push heroku master
Counting objects: 9, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (5/5), 580 bytes, done.
Total 5 (delta 2), reused 0 (delta 0)
-----< Heroku receiving push
-----< Rails app detected
       Compiled slug size is 92K
-----< Launching..... done
       http://note-taker-pg.heroku.com deployed to Heroku
To git@heroku.com:note-taker-pg.git
   449e6c2..3639dce  master -> master

The default deployment at Heroku is free and uses one "dyno" compute unit. It also provides 5MB of storage for your database. A dyno is similar to a mongrel process in that it handles one request at a time. Just as you can run a cluster of mongrels on your own server, you can use the Heroku web interface to increase or decrease the number of dynos allocated to your web application.

When I am done with a deployed test application, I use Heroku's web interface to delete it. This frees up resources, a polite gesture when using the free deployment option.

Code Download

For Further Reading

About the Author

Mark Watson is a consultant living in the mountains of Central Arizona with his wife Carol and a very feisty Meyers Parrot. He specializes in web applications, text mining, and artificial intelligence development. He is the author of 16 books and writes both a technology blog and an artificial intelligence blog.

Page 2 of 2

This article was originally published on January 21, 2010

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date