Code Academy Week 2 Notes Post 01

Here are my notes for the first day of week 2 at Code Academy.

 

2012-01-17_08.07.38
Methods:

Classes: Fundamental

Ruby On Rails:
Why use it?
Database-Backed Web Applications
Convention Over Configuration
Agile Development

Rails quickstart:

go to http://localhost:3000

Webbrick is great for development. There is Apache, nginx for production.

The index arg is a method that will be created in the controller
There is also an index file in app/views/landmarks
Use plural for controllers
So we could view this at http://localhost:3000/landmarks/index

For a request, the route config will pick a controller

Models: software objects that represent real-world objects. We use Ruby classes to define models.
They go in the $APP_NAME/app/models folder
In models folder, create a file landmakrk.rb
Controllers are plural, models are singular
Make a model class, then fire up the “rails console”
like “rails server” you should be at the top level of the app
In the html.erb files:

to print output of Ruby code:

This is like string interpolation, but it is called “embedding Ruby” when it is in an erb file
The variables are local to that view file. They die once the view is rendered
Controllers job is to decide what data to present, view decides how to present

Each method in controller corresponds to a view file – that is pairing
Instance variables in method are readable by the view
Now getting data from database

could run for help:

This creates a file in db/migrate/20120117171507_create_landmarks.rb
and app/models/landmark.rb
Here is the migration file:

How to run it? with rake (or ruby make)

Now go to rails console:

The object has fields, even though the model class is pretty empty

you get the attr_accessors automatically:

You can add stuff to database via the console

To list rake targets: rake –tasks

To delete database and recreate it with same structure:

Now, how do I make a page for each landmark?
How about a show page?
add

in config/routes.rb
So we need a file app/view/landmarks/show.html.erb and a show method in the Landmarks controller
Action: Is method in controller and corresponding view page

Image from Wikimedia, assumed allowed under Fair Use. Image from the Vatican Virgil, a 5th century manuscript of poems by Virgil.