Here are my notes for the first day of week 2 at Code Academy.
Ruby On Rails:
Why use it?
Database-Backed Web Applications
Convention Over Configuration
rails new myapp cd myapp rails server
go to http://localhost:3000
Webbrick is great for development. There is Apache, nginx for production.
rails generate controller landmarks index
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:
<% ruby code %>
to print output of Ruby code:
<%= hancock.name %>
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
rails generate model product name:string color:string rails generate model (generator) product (model name) name:string (model attribute) color:string (model attribute)
could run for help:
rails generate model -h rails generate model landmark name:string cost:integer
This creates a file in db/migrate/20120117171507_create_landmarks.rb
Here is the migration file:
class CreateLandmarks < ActiveRecord::Migration def change create_table :landmarks do |t| t.string :name t.integer :cost t.timestamps end end end
How to run it? with rake (or ruby make)
Now go to rails console:
ericm@finance:~/ruby/codeAcademy/tth/second$ rails console Loading development environment (Rails 3.1.0) ruby-1.9.2-p290 :001 > ballpark = Landmark.new => #<Landmark id: nil, name: nil, cost: nil, created_at: nil, updated_at: nil> ruby-1.9.2-p290 :002 >
The object has fields, even though the model class is pretty empty
you get the attr_accessors automatically:
ruby-1.9.2-p290 :001 > ballpark = Landmark.new => #<Landmark id: nil, name: nil, cost: nil, created_at: nil, updated_at: nil> ruby-1.9.2-p290 :002 > ballpark.name = "Wrigley Field" => "Wrigley Field" ruby-1.9.2-p290 :003 > ballpark.cost = 10 => 10 ruby-1.9.2-p290 :004 > ballpark => #<Landmark id: nil, name: "Wrigley Field", cost: 10, created_at: nil, updated_at: nil>
You can add stuff to database via the console
ruby-1.9.2-p290 :005 > ballpark.save SQL (54.8ms) INSERT INTO "landmarks" ("cost", "created_at", "name", "updated_at") VALUES (?, ?, ?, ?) [["cost", 10], ["created_at", Tue, 17 Jan 2012 17:23:53 UTC +00:00], ["name", "Wrigley Field"], ["updated_at", Tue, 17 Jan 2012 17:23:53 UTC +00:00]] => true
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?
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.