RoR Quick Start
From KdjWiki
NOTE: I am assuming you have installed Ruby and the Rails framework, and have tested it to ensure it works. i.e. $ ruby -v returns the version.
Contents |
Create Database
Assume created database MyDatabase
Create Tables
Assume created table customers
NOTE: Table should be plural of model.
NOTE: Table needs to have a column id as the primary key.
NOTE: If table includes created_on (timestamp) and/or updated_on (timestamp) rails will maintain them automagically.
Create Rails Application
$ cd /var/www/ $ sudo rails MyWebsite
create create app/apis create app/controllers create app/helpers [...]
Configure for Database
$ cd MyWebsite $ sudo nano -w config/database.yml
development:
adapter: mysql
database: MyDatabase
host: localhost
username: my_user
password: my_password
Initial Testing
Testing with WEBrick server
$ sudo ruby script/server -d
=> Rails application started on http://0.0.0.0:3000 [2006-03-29 17:19:16] INFO WEBrick 1.3.1 [2006-03-29 17:19:16] INFO ruby 1.8.4 (2005-12-24) [i686-linux]
Then browse to http://localhost:3000
Testing with Apache
Create virtual host
$ sudo nano -w /etc/apache2/vhosts.d/MyWebsite.conf
Listen 127.0.0.1:3000
<VirtualHost 127.0.0.1:3000>
ServerName my_website
DocumentRoot /var/www/MyWebsite/public/
ErrorLog /var/www/MyWebsite/log/apache.log
<Directory /var/www/MyWebsite/public>
Options ExecCGI FollowSymLinks
AddHandler cgi-script .cgi
AllowOverride all
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Configure for FastCGI
$ sudo nano -w public/.htaccess
RewriteRule ^(.*)$ dispatch.cgi?$1 [QSA,L]
becomes
RewriteRule ^(.*)$ dispatch.fcgi?$1 [QSA,L]
Test
Browse to http://localhost:3000
Built In Scaffolding
Create Scaffold
Remembering we are using the database table customers (so we have the model customer)
$ sudo ruby script/generate model customer
exists app/models/ exists test/unit/ exists test/fixtures/ create app/models/customer.rb [...]
$ sudo ruby script/generate controller customer
exists app/controllers/ exists app/helpers/ create app/views/customer [...]
Use Scaffold
$ sudo nano -w app/controllers/customer_controller.rb
class CustomerController < ApplicationController
model :customer
scaffold :customer
end
Test Scaffold
Browse to http://localhost:3000/customer/
Customise Scaffold
$ sudo ruby script/generate scaffold customer
dependency model [...] create app/views/customer exists test/functional/ [...]
Now you can modify files such as:
- app/views/customers/list.rhtml
- app/controllers/customers_controller.rb
- public/stylesheets/scaffold.css
Notes:
To denote a one-to-many relationship in the model:
class Order < ActiveRecord::Base
belongs_to :customer
end
and
class Customer < ActiveRecord::Base
has_many :order
end