Tuesday, January 25, 2005

Ruby on Rails, first glance

Below are notes and thoughts as I went through the Rails tutorial at
http://www.onlamp.com/pub/a/onlamp/2005/01/20/rails.html?page=1 Database was mysql 4.2.3 and all commands were executed from mysql console.

First,
emerge ruby rubygems
gem install rails --remote

(get rid of proxy in .bashrc, or problem)
"rails " creates all of the files for a rail project in directory
has builtin webserver (WEBrick), invoked from "ruby script/server"
starts up on port 3000
some notes and suggestions on rails presented on default page
"Rails tries very hard to minimize the number of decisions you have to make and to eliminate unnecessary work. When you used the rails helper script to create your empty application, it created the entire directory structure for the application (Figure 9). Rails knows where to find things it needs within this structure, so you don't have to tell it. Remember, no configuration files!"
--http://www.onlamp.com/pub/a/onlamp/2005/01/20/rails.html?page=2
broken on ruby script\generate controller MyTest
ruby: No such file or directory -- script/generate (LoadError)
need to be in the project directory to run script, woops
"* The controllers subdirectory is where Rails looks to find controller classes. A controller handles a web request from the user.
* The views subdirectory holds the display templates to fill in with data from our application, convert to HTML, and return to the user's browser.
* The models subdirectory holds the classes that model and wrap the data stored in our application's database. In most frameworks, this part of the application can grow pretty messy, tedious, verbose, and error-prone. Rails makes it dead simple!
* The helpers subdirectory holds any helper classes used to assist the model, view, and controller classes. This helps to keep the the model, view, and controller code small, focused, and uncluttered."--http://www.onlamp.com/pub/a/onlamp/2005/01/20/rails.html?page=2

create the tutorial db with:
create database cookbook

add the sysid(PK, autoincrement):
create table cookbook.recipes (sysid int AUTO_INCREMENT key);

add the other columns:
alter table cookbook.recipes add(title varchar(256) not null,instructions text)
Model for this table is generated by: "ruby script\generate model Recipes"
Controller: "ruby script/generate controller Recipes":
adding "scaffold :recipes" will enable CRUD operations...need more information as to what this is doing. Creating an object that reflects the table?
http://127.0.0.1:3000/recipes/new brings up a generated form to enter all the data corresponding to the fields in the table. Pretty slick.
http://127.0.0.1:3000/recipes/list shows the available records and the ability to Show Edit Destroy. Very slick, this was cake to get up and running and could be very handy for quick db form apps.
Now updating the db table structure:
"alter table cookbook.recipes add(description varchar(256), date datetime);"
Hitting the "new" page again immediately reflects new fields and a nifty date picker.
Can override the "list" view/page by creating an .rhtml template in the views directory (list.rhtml)

created a second table, create table categories(sysid int auto_increment key, kind varchar(55));
ruby script/generate controller Category
ruby script/generate model Category
added "scaffolding :category" to category_controller.rb, can now create records at http://127.0.0.1:3000/category/new

If the pk id is "id" then Rails won't generate a form input or display it. Thus, the sysids in the entries above allowed entries. When I dropped the table and retried with "id" then it becomes hidden. AND, the Show,Edit,Destroy now work.

now to add a "category" to a recipe: alter table recipes add(category_id int(6) not null);
update recipes.rb adding:
belongs_to :category
and category.rb:
has_many :recipes
By adding the above lines code is generated to navigate these relationships. Question is, can Ruby identify db-constrained relationships and do the same thing?

added recipes_controller.rb:
def edit
@recipes=Recipe.find(@params["sysid"])
@categories=Category.find_all
end
then created edit.rhtml in the app/recipes/view directory

need to recreate recipes table with correct id instead of sysid:
create table recipes(id int auto_increment key,title text,instructions text, description text, date date,category_id int(6));

Followed the rest of the tutorial and results were similar (though I changed a couple of column names and edited the code as I progressed).

So, Rails looks to be quite a promising way to generate quick webpages around an existing data model. I'm going to design a database for keeping track of my books and see how quickly I can use Rails to get it churned out into a web app.

No comments: