Rails: How to get started

December 7, 2007

First step to rails… Installing it.

Okay, so, it’s much easier if you have RubyGems installed, and if you do:

[sudo] gem install -y rails

You can then start a project.


rails [--database=sqlite3] [options] [projectname]

You could read the README, but that’s complicated and this is a lot easier.
First of all, “cd” into your new project.

cd [projectname]

Models

Then, you can start with a Model.
In Rails, Models are like collections of objects. Behind them is a technology called ActiveRecord. Using this, we can…

  • create users… (script/generate model user name:string password:string about:string)

And other thing-a-ma-bobs.
IMPORTANT! Make sure you edit the db/migrate/[number]_create_[modelname].rb file first so that where it says create_table(…) there is a line like this right under it.

t.column :id, :integer, :null => false

Strangely, the generator doesn’t automatically put this in… I’ll have to file a bug report…

Rendering the information (a.k.a. Controllers)

Well, there is something called scaffolding which you could put in your app/controllers/[controllername]_controller.rb file… But I’m gonna teach you the hard , kind-of easy way.

First, the same way we generate a model:

script/generate controller [controllername]
# ...or...
script/generate scaffold [controllerandmodelname]

The app/controllers/[controllername]_controller.rb file should look like this

class [CamelCaseControllerName] < ApplicationController
layout "[layoutname]"
def [viewmethod]
[helping-hands]
end
#...
end

Views

I actually prefer using Haml rather than the usual RHTML, so go ahead and use RubyGems:

[sudo] gem install -y haml
#and
cd ..
haml --rails [projectname]
cd [projectname]

Great, your project is now Haml-enabled. What do I do now?

Fire up app/views/layouts/[layoutname].haml, and put in

!!!
%html
%head
%title= @title
%body
= yield

And, so, a beginning.

You may now delete everything in the public/ folder except the folders images/, stylesheets/, and javascripts/. After that:

script/server

Then fire up Firefox and go to:

http://localhost:3000/[controllername]/

Something should show if you configured everything correctly.


Numbers in Ruby

December 4, 2007

Okay, something new for my beginners, NUMBERS!

So, it sounds stupid, of course every programming language has numbers!


2 + 2 #=> 4

Yup. Works pretty much in every programming language.

Lisp:

(+ 2 2)

Why not 50×50?


50*50 #=> 2500

Why not 2 into 3?


3/2 #=> 1

??? What just happened?

It seems ruby understands many types of numbers…


3.0/2.0 #=> 1.5

Cool…


Ruby!

November 14, 2007

Now I will turn you into my evil minions!

Okay, so for some of those noobs out there, here’s your first lesson.

Tradition has it that the first thing you should learn is “Hello, world!”. Let’s try this. Don’t worry, Ruby is designed for simplicity, so fire up IRB and let’s get started!

puts "Hello, World!"

Yes! Yes! You’ve got it! Now compare to the popular C language.

#include
using namespace std;
int main() {
cout << "Hello, World!" << endl;
}

Now, isn’t that easier? Muahahahahahaha! You are under my control now!


Follow

Get every new post delivered to your Inbox.