Tuesday, August 14, 2007
Short tutorial for creating a Ruby on Rails app
Assumptions:
* You have Ruby and Rails already installed.
* You are using mongrel, webrick for the web server.
* Using sqlite3 for the database and you have it installed
Before starting I would suggest deciding on using some version control. RCS is better than nothing.
Now decide on the app name. This is the top level app name. It can have related stuff below it... Example your app could be to log all data about your health. Lets call it myhealth. So run
rails myhealth
That creates the base framework for the app. Now go into the new myhealth directory.
Edit config/database.yml and for sqlite3 use
mode: sqlite3
dbfile: db/myhealth_development
(Note: if you call it myhealth_development.db RoR does not seem to find it)
For this health example we could have several things such as Eating, Sleeping, Illness (could include headaches), as well as various types of excercise. I will just use one type of exercise, cycling, which will log ride data...
Now we need to create the model which ties everything to the database. This will create several things including the skeleton migration script for setting up the database.
./script/generate model Ride
Edit the initial migration in db/migrations/. You create new ones with
./script/generate migration name_of_migration_script
(Note: underscores or camelcase is required)
The migration script should look like
Now run
rake db:migrate
This will create the database and tables.
(Note: By default you are in development mode... if you want to generate the db for others use - rake environment RAILS_ENV=test db:migrate)
Create the controller and views
./script/generate controller Rides list view new edit
This creates the different "pages" (views) as well as the controller script which talks to the model.
Before starting any coding I suggest putting everything in version control. You probably want to exclude the log/, tmp/ and db/ directory. You can backup the database (and add the backup to version control) with sqlite3 path/to/db .dump
After that you can start editing the model, controller, and views. Here is the view.rhtml:
and here is the controller code for this view:
See the reference links below for more detailed information.
TIPS
Test and manipulate stuff from the console
./script/console
Run rake (with no arguments) to run some tests against all your stuff.
REFERENCES
Tutorials, etc
http://www.tutorialspoint.com/ruby-on-rails/
RoR Wiki - Tutorial
http://www.sitepoint.com/article/ruby-on-rails
Migrations
RoR Wiki - UsingMigrations
Ruby on Rails Migrations Reloaded/
API docs for ActiveRecord/Migration
Oracle article on Rails Migrations
Rails migrations using sqlite3 on Mac OS X
Misc
RoR Wiki - RailsBestPractices
Calendar Date Select: A Lightweight, Prototype-based Date/Time Picker for Rails Developers
* You have Ruby and Rails already installed.
* You are using mongrel, webrick for the web server.
* Using sqlite3 for the database and you have it installed
Before starting I would suggest deciding on using some version control. RCS is better than nothing.
Now decide on the app name. This is the top level app name. It can have related stuff below it... Example your app could be to log all data about your health. Lets call it myhealth. So run
rails myhealth
That creates the base framework for the app. Now go into the new myhealth directory.
Edit config/database.yml and for sqlite3 use
mode: sqlite3
dbfile: db/myhealth_development
(Note: if you call it myhealth_development.db RoR does not seem to find it)
For this health example we could have several things such as Eating, Sleeping, Illness (could include headaches), as well as various types of excercise. I will just use one type of exercise, cycling, which will log ride data...
Now we need to create the model which ties everything to the database. This will create several things including the skeleton migration script for setting up the database.
./script/generate model Ride
Edit the initial migration in db/migrations/. You create new ones with
./script/generate migration name_of_migration_script
(Note: underscores or camelcase is required)
The migration script should look like
class CreateRides < limit =""> 40
t.column :comment, :string
end
end
def self.down
drop_table :rides
end
end
Now run
rake db:migrate
This will create the database and tables.
(Note: By default you are in development mode... if you want to generate the db for others use - rake environment RAILS_ENV=test db:migrate)
Create the controller and views
./script/generate controller Rides list view new edit
This creates the different "pages" (views) as well as the controller script which talks to the model.
Before starting any coding I suggest putting everything in version control. You probably want to exclude the log/, tmp/ and db/ directory. You can backup the database (and add the backup to version control) with sqlite3 path/to/db .dump
After that you can start editing the model, controller, and views. Here is the view.rhtml:
<h2><%= @ride.name %></h2>
<p>
<br />
Trip Dist: <%= @ride.trip_distance %><br />
Trip Time: <%= @ride.trip_time_t.to_s %>(<%= @ride.trip_time %>)<br />
Avg Speed: <%= @ride.avg_speed %><br />
Max Speed: <%= @ride.max_speed %><br />
Riders: <%= @ride.riders %><br />
Comments:
<blockquote>
<%= @ride.comment %>
</blockquote>
<hr noshade />
<%=link_to "List", :action => "list" %>
<%= link_to "Edit", :action => "edit", :id => @ride.id %>
<%= link_to "Delete", { :action => "destroy", :id => @ride.id} ,
:confirm => "Are you sure?", :method => "post" %>
</p>
and here is the controller code for this view:
def view
@ride = Ride.find(params["id"])
end
See the reference links below for more detailed information.
TIPS
Test and manipulate stuff from the console
./script/console
Run rake (with no arguments) to run some tests against all your stuff.
REFERENCES
Tutorials, etc
http://www.tutorialspoint.com/ruby-on-rails/
RoR Wiki - Tutorial
http://www.sitepoint.com/article/ruby-on-rails
Migrations
RoR Wiki - UsingMigrations
Ruby on Rails Migrations Reloaded/
API docs for ActiveRecord/Migration
Oracle article on Rails Migrations
Rails migrations using sqlite3 on Mac OS X
Misc
RoR Wiki - RailsBestPractices
Calendar Date Select: A Lightweight, Prototype-based Date/Time Picker for Rails Developers
Labels: rails, ror, ruby, tutorial