Home > On the Ground > On the Rails: Create a Simple CRUD App, Set Up a Database

On the Rails: Create a Simple CRUD App, Set Up a Database

December 29th, 2009 Jeremy Merrill

Once you have everything installed, it’s actually pretty easy to create a basic application in Rails that can perform the four basic CRUD operations — Create, Read, Update, Delete — on a set of data.

Before we create the app, though, let’s briefly describe what our data is going to look like. Let’s say we want to store some basic information about all of the books we have in our possession. Most books have, among other things, a title, an author, a publishing company, a copyright year, and an ISBN number (a unique identifier). If our database table is going to be composed of books, then each record in that table is going to have each one of the above attributes, stored as strings of text.

Create CRUD App

So, let’s first create our initial, empty skeleton of a Rails app.  From your home directory, or whatever directory you want to store your Rails projects in, issue the following command:

rails railstest -d postgresql

This will create a folder named “railstest” and populate it with all of the wonderful goodies that Rails provides as a foundation for writing a web-based application that follows the Model-View-Controller architectural pattern. The “-d postgresql” part pre-configures the app to use the relational database system we have chosen, PostgreSQL. (If you wanted to use MySQL or something else, you could just as easily specify a command-line argument for whatever you’ve chosen instead. Type “rails –help” to see all the options.)

Now, we’re going to take our foundation and build a CRUD app on top of it. But we’re not going to type a single line of Ruby code. We’re going to make use of scaffolding. (Later, we’ll want to avoid scaffolding like the plague, and build our apps by actually writing our own Ruby code, but we’re trying to get up and running ASAP, so we’ll take the easy way for now.)

Issue the following commands from the directory containing your Rails project(s):

cd railstest
./script/generate scaffold book title:string author:string publisher:string copyright_year:string isbn:string

This will create some code files — models, views, and controllers — and a database migration, which describes how to set up our database schema. We’ll be invoking rake (a build program for Ruby) a little later to do just that.

Set Up Database

Before we do that, though, we should create a database cluster, launch the database server, create a new database user, and finally create the database. (You may have taken note of some instructions that appeared in your Terminal after you installed PostgreSQL. If you did, some of the commands in this step should look familiar.)

First, we need to create and initialize the data directory, or database cluster. Issue the following three commands in order:

sudo mkdir -p /opt/local/var/db/postgresql84/railstest
sudo chown postgres:postgres /opt/local/var/db/postgresql84/railstest
sudo su postgres -c '/opt/local/lib/postgresql84/bin/initdb -D /opt/local/var/db/postgresql84/railstest'

Next, launch the database server. Open a new Terminal tab or window, and then issue the following command:

sudo su postgres -c '/opt/local/lib/postgresql84/bin/postgres -D /opt/local/var/db/postgresql84/railstest'

Next, create a new database user (or role):

sudo su postgres -c '/opt/local/lib/postgresql84/bin/createuser -S -d -R railstest'

Finally, create the development database:

sudo su postgres -c '/opt/local/lib/postgresql84/bin/createdb -O railstest railstest_development'

Now, let’s run rake and create our schema:

rake db:migrate

Just Push Play

We’re almost there! Time to start up our web server and Ruby application layer. At the moment, all we have is WEBrick, which comes with Ruby. (Note: WEBrick is a quick and dirty web server solution that will run our simple app and get us to the end of this part of the tutorial, but it should not be used for anything more complicated, and certainly not in a production environment.) Run the following command in a new Terminal:

./script/server

Now, open your web browser and go to:

http://localhost:3000

You should see a Rails welcome screen. Next, go to:

http://localhost:3000/books

You should see our CRUD app! You should now be able to Create, Read (or Show), Update (or Edit) and Delete (or Destroy) books. The data is stored in our PostgreSQL database instance we created above.

Next Episode

Next time, we’ll put the code under version control locally using git, set up a remote repository on GitHub, and then push our code to that remote location. See you then!

Categories: On the Ground Tags:
Comments are closed.
Rising damp download movie Return of sabata download movie Adoration download movie Rising damp download movie Return of sabata download movie Adoration download movie The village download movie Enemy at the gates download movie Riders download movie The guardian download movie Ten dead men download movie Over her dead body download movie Out of africa download movie Soul survivors download movie The fallen ones download movie