Ruby on Rails Kickstart
So the first public training is over now. I've learned a couple of things. I think this was one of the most difficult trainings I've ever been through – and even though it seems better after getting some distance there was definitely a few things we could have done better. What I really learned here is that even though Ruby on Rails is easy, it may be not so easy and really helps if you have a programming experience.
In any case here are the steps we covered in the first session. The whole point was to just get a basic scaffold running. There was a time I thought it was easy. I don't anymore :-).
- Install everything - here's one way to do it on Windows - it worked on most of the machines but not all. Things to watch out for: in few cases ruby bin dir wasn't added to the path, in others Mysql wouldn't start yet in others Apache wouldn't start. Most of it were XP machines but I guess there were differences. As pretty much all of our development is done on Linux and MacOS I just have more experience with those platforms.
- What you will need - 2 command prompt windows, text editor, mysql (or any other database) and browser
-
Create a directory for your applications - on Windows type this:
cd c:\ mkdir rails cd rails
and here it is for MacOS / Linux:cd ~ mkdir rails cd rails

-
Generate the basic application structure:
rails test
You should see a bunch of lines getting generated:
As a result you should be able to see the following directory structure generated:
-
Change to the generated directory and start the server to see if we're good to go:
ruby script/server
You should see something like this - confirming that the server has been started:
Now open up a browser and navigate to http://localhost:3000. You should see this page coming up:
To stop the server you can press Ctr+C. To start it again just press arrow up which should repeat the last command (i.e. ruby script/server). -
From now on we will be using the another command line window. Leave the server running in the first window.
-
Set up the database connection. The database connection parameters are set up in config/database.yml file. On most platforms it should be geneated properly,
but on some you may have to add the host: localhost line. At the end it should look something like this
-
Now we can create a new database. Type this in the second command prompt window:
mysql -u root create database test_development; create database test_test; \q
You should see something like this:
To verify that everything is okay run rake db:migrate and you shouldn't see any error messages. If you see warning about require_gem being obsolete - it's okay. -
Now we generate some basic model:
ruby script/generate model product

-
Create a Migration
I will get to the whole migration concept in some future installments - for now just open up db/migrate/001_create_products.rb and add the following lines:
class CreateProducts < ActiveRecord::Migration def self.up create_table :products do |t| t.column :name, :string t.column :description, :text t.column :price, :numeric t.column :image_url, :string end end def self.down drop_table :products end end -
Now the hard part - run the migration. Running the migration will make the changes in the database. You will HAVE to have your database as well as database.yml properly set-up, otherwise this won't work.
rake db:migrate
If everything went well you should see something like this:
-
Create a controller.
ruby script/generate controller products
Just like always, you should see some lines informing you about the files being generated. Here are the new files that got generated:
-
Now just add this line to the generated controller so that it looks like this:
class ProductsController < ApplicationController scaffold :product end
-
And here we are :-) Navigate the browser to http://localhost:3000/products/list and you should see this:
Go ahead and click on New and add a new product:
-
Now we've got the rice (or meat and potatos if you're from that culture) - now just make it look better. We'll add a very basic layout and some basic CSS.
Both of them are from the Agile Web Development with Ruby on Rails book examples. You need to create a new file under app/views/layouts/application.rhtml and add the following to it:
Get the Style Sheet Here
And here's the end product:

And that's all. Easy or not - if you can do this you will be able to do anything. If you were not able to follow or the whole darn thing just wouldn't work - let me know where you had troubles.