Since Rails is now in Debian Unstable(sid), not much to say about installation. It’s as easy as:
apt-get install rails
The blog application needs two tables: posts, comments. Following the Rails recommended table and column naming conventions is a good idea and will save even more time later. Every table should be named in the plural, have a primary key called “id”, set as auto increment. Links to other tables should be in the form “singualOfForeignTableName_id”. Rails will automatically maintain fields called created_at/created_on and/or updated_at/updated_on(timestamp). A field called lock_version(integer default of 0) should also be included to allow Rails to do optimistic locking in a multi-user system.
Creating the Database and Tables
create database Blog;CREATE TABLE `posts` ( `id` smallint(5) unsigned NOT NULL auto_increment, `title` text NULL default '', `body` text NULL default '', `created_on` timestamp(14) NOT NULL, `updated_on` timestamp(14) NOT NULL, PRIMARY KEY (`id`) ) TYPE=MyISAM COMMENT='List of posts';CREATE TABLE `comments` ( `id` smallint(5) unsigned NOT NULL auto_increment, `title` text NULL default '', `body` text NULL default '', `post_id` smallint(5) unsigned NOT NULL, `created_on` timestamp(14) NOT NULL, `updated_on` timestamp(14) NOT NULL, PRIMARY KEY (`id`), INDEX `post_id` (`post_id`) ) TYPE=MyISAM COMMENT='List of comments';
Rails generates a good bit of code for you. Now that we have our database/tables in place we can run the Rails script to create our initial Blog application shell. The following command will create a Blog subdirectoryand application shell.
$ rails Blog
Now we need to tell Rails how to connect to our database by editing the database.yml file.
$ cd Blog$ vi config/database.yml
The file will look something list this:
development: adapter: mysql database: rails_development host: localhost username: root password: test: adapter: mysql database: rails_test host: localhost username: root password:production: adapter: mysql database: rails_production host: localhost username: root password:
You will need to edit the development section to something like this:
development: adapter: mysql database: Blog host: localhost username: root password:
change your database/host/username/password as appropriate.Next we will run a few more code generation script to produce a model and controller shell for our posts table.
$ ruby script/generate model Post$ ruby script/generate controller post
The model name must start with a capital letter and should be the singular name of the related table.Now we need to link our controller to the model. Edit the post_controller.rb that was generated for you:
$ vi app/controllers/post_controller.rb
class PostController < ApplicationController model :postend
We are now ready to test the application. Rails will run under Apache, but for this example we will use the build in WEBrick http server. In the /Blog directory:
$ ruby script/server
You should now be able to connect to your application from http://localhost:3000 and then access the application http://localhost:3000/post.
Unknown actionNo action responded to index
Not very nice, but it's working. Lets make it nicer by allowing Rails to fill in the details.Edit post_controller.rb again and add "scaffold :post".
class PostController < ApplicationController model :post scaffold :postend
Now access http://localhost:3000/post/
Listing postsTitle Body Created on Updated onNew post
You've got a functioning application. More to come.