お気に入りのインテリア雑貨のお店、ノアシス@神戸
レトロポップでモダンなデザインは見てるだけで楽しーだす。。

Tuesday, March 22, 2005

Developing a match-making website with Ruby on Rails, Part 1

Developing a match-making website with Ruby on Rails, Part 1

By Saito Hiroyuki, 21/03/2005

I believe that learn by teaching is the most effective way to learn. Please let me know any mistakes if you find.

After reading excellent Rails tutorials on O'reilly and being surprised with scaffolding generator, I decided to create my first Rails application, a match-making website, which may generate a million dollar.

I use Login generator:
gem install --source http://dist.leetsoft.com login_generator

Create a new Rails application with:
rails Match

Create database 'match' on MySQL. Open database.yml and change development database name into 'match'.

Rails is a model centric web application framework, so database schema is the most important.

# Dump of table users
# ------------------------------

CREATE TABLE `users` (
`id` int(11) NOT NULL auto_increment,
`login` varchar(80) default NULL,
`password` varchar(40) default NULL,
PRIMARY KEY (`id`)
) TYPE=MyISAM;

# Dump of table profiles
# ------------------------------

CREATE TABLE `profiles` (
`id` int(11) unsigned NOT NULL auto_increment,
`title` varchar(50) NOT NULL default '',
`description` text NOT NULL,
`user_id` int(11) unsigned NOT NULL default '0',
`created_on` timestamp(14) NOT NULL,
`filename` varchar(50) default NULL,
`picture` blob,
PRIMARY KEY (`id`)
) TYPE=MyISAM;

# Dump of table kisses
# ------------------------------

CREATE TABLE `kisses` (
`id` int(11) unsigned NOT NULL auto_increment,
`subject` varchar(50) NOT NULL default '',
`comment` text NOT NULL,
`profile_id` int(11) unsigned NOT NULL default '0',
`user_id` int(11) unsigned NOT NULL default '0',
`created_on` timestamp(14) NOT NULL,
PRIMARY KEY (`id`)
) TYPE=MyISAM;

On 'kiss' table, 'profile_id' means which profile the message (kiss) is sent to (recipient of the message), 'user_id' which user sent the message (issuer of the message).

Do the followings:
rails script/generate login Account
rails script/generate scaffold Profile
rails script/generate scaffold Kiss

Let's specify the associations for models:

class User < ActiveRecord::Base
has_one :profile
has_many :kisses

class Profile < ActiveRecord::Base
belongs_to :user
has_many :kisses

class Kiss < ActiveRecord::Base
belongs_to :profile
belongs_to :user

With the associations above, you can do:
Access the login name of issuer of the message id number 2 (from the link containing :id => '2'):
@kiss = Kiss.find(@params['id'])
@kiss.user.login
Access the user id of recipient of the message:
@kiss.profile.user.id

Edit application.rb (/app/controllers/) to have login system available in all controllers:

require_dependency "login_system"
class ApplicationController < ActionController::Base
include LoginSystem
model :user
end

Insert 'before_filer' to activate login system in profiles_controller.rb:

class ProfilesController < ApplicationController
before_filter :login_required, :except => [:index]

Create a top page, index.rhtml (/app/views/profiles/), like this:
<h1>Wahoo Personals!</h1>
<%= link_to 'Register', :controller => 'account', :action => 'signup' %> |
<%= link_to 'Login', :controller => 'account', :action => 'login' %>

Run a WEBrick server:
ruby script/server

Access to your match-making application:
http://localhost:3000/profiles/

Congraturations!! Now you have a top page and can registar/login.
This is the end of Part 1.