Demo App

$ rails new demo_app
$ cd demo_app

Users Resource

$ rails generate scaffold User name:string email:string                                 #id will be generated automatically as primary key
    invoke  active_record
      create    db/migrate/20130715233046_create_users.rb
      create    app/models/user.rb
      invoke    test_unit
      create      test/models/user_test.rb
      create      test/fixtures/users.yml
      invoke  resource_route
       route    resources :users
      invoke  scaffold_controller
      create    app/controllers/users_controller.rb
      invoke    erb
      create      app/views/users
      create      app/views/users/index.html.erb
      create      app/views/users/edit.html.erb
      create      app/views/users/show.html.erb
      create      app/views/users/new.html.erb
      create      app/views/users/_form.html.erb
      invoke    test_unit
      create      test/controllers/users_controller_test.rb
      invoke    helper
      create      app/helpers/users_helper.rb
      invoke      test_unit
      create        test/helpers/users_helper_test.rb
      invoke    jbuilder
      create      app/views/users/index.json.jbuilder
      create      app/views/users/show.json.jbuilder
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/
      invoke    scss
      create      app/assets/stylesheets/users.css.scss
      invoke  scss
      create    app/assets/stylesheets/scaffolds.css.scss

$ bundle exec rake db:migrate             #  migrate the database using Rake
==  CreateUsers: migrating ====================================================
-- create_table(:users)
   -> 0.0210s
==  CreateUsers: migrated (0.0211s) ===========================================


Rake is Ruby make, a make-like language written in Ruby.
$ bundle exec rake -T db
$ bundle exec rake -T

/usersindexpage to list all users
/users/1showpage to show user with id 1
/users/newnewpage to make a new user
/users/1/editeditpage to edit user with id 1

Add a new user (The green welcome message is accomplished using the flash)

This full suite of controller actions, represents the implementation of the REST architecture in Rails, which is based on the ideas of representational state transfer.

HTTP requestURLActionPurpose
GET/usersindexpage to list all users
GET/users/1showpage to show user with id 1
GET/users/newnewpage to make a new user
POST/userscreatecreate a new user
GET/users/1/editeditpage to edit user with id 1
PATCH/users/1updateupdate user with id 1
DELETE/users/1destroydelete user with id 1
 RESTful routes provided by the Users resource

REpresentational State Transfer (REST)

If you read much about Ruby on Rails web development, you’ll see a lot of references to “REST”, which is an acronym for REpresentational State Transfer. REST is an architectural style for developing distributed, networked systems and software applications such as the World Wide Web and web applications. Although REST theory is rather abstract, in the context of Rails applications REST means that most application components (such as users and microposts) are modeled as resources that can be created, read, updated, and deleted—operations that correspond both to the CRUD operations of relational databases and four fundamental HTTP request methods: POST, GET, PATCH, and DELETE. (We’ll learn more about HTTP requests in Section 3.2.1 and especially Box 3.3.)

Model inherits from active record.
Variables that start with the @ sign, called instance variables, are automatically available in the view;

Microposts Resource

$ rails generate scaffold Micropost content:string user_id:integer
     invoke  active_record
      create    db/migrate/20130716191611_create_microposts.rb
      create    app/models/micropost.rb
      invoke    test_unit
      create      test/models/micropost_test.rb
      create      test/fixtures/microposts.yml
      invoke  resource_route
       route    resources :microposts
      invoke  scaffold_controller
      create    app/controllers/microposts_controller.rb
      invoke    erb
      create      app/views/microposts
      create      app/views/microposts/index.html.erb
      create      app/views/microposts/edit.html.erb
      create      app/views/microposts/show.html.erb
      create      app/views/microposts/new.html.erb
      create      app/views/microposts/_form.html.erb
      invoke    test_unit
      create      test/controllers/microposts_controller_test.rb
      invoke    helper
      create      app/helpers/microposts_helper.rb
      invoke      test_unit
      create        test/helpers/microposts_helper_test.rb
      invoke    jbuilder
      create      app/views/microposts/index.json.jbuilder
      create      app/views/microposts/show.json.jbuilder
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/
      invoke    scss
      create      app/assets/stylesheets/microposts.css.scss
      invoke  scss
      identical    app/assets/stylesheets/scaffolds.css.scss

$ bundle exec rake db:migrate
==  CreateMicroposts: migrating ===============================================
-- create_table(:microposts)
   -> 0.0281s
==  CreateMicroposts: migrated (0.0281s) ======================================


To put constaints on model e.g. maximum size of a field
$ vim ./app/models/micropost.rb
class Micropost < ActiveRecord::Base 
    validates :content, length: { maximum: 140 }

A user has_many microposts

A user has many microposts.
$ vim app/models/user.rb
class User < ActiveRecord::Base
  has_many :microposts

A micropost belongs to a user
$vim app/models/micropost.rb
class Micropost < ActiveRecord::Base
  belongs_to :user
  validates :content, length: { maximum: 140 }

Test in Relation in Rails Console

$ rails console
>> firstUser = User.first
>> firstUser.microposts                 # association facilities in Active Record
>> exit   OR Ctrl+d

It is by inheriting from ActiveRecord::Base that our model objects gain the ability to communicate with the database, treat the database columns as Ruby attributes, and so on.

Model inherittance

Controllers inheritance

by inheriting ultimately from ActionController::Base both the Users and Microposts controllers gain a large amount of functionality, such as the ability to manipulate model objects, filter inbound HTTP requests, and render views as HTML. Since all Rails controllers inherit from ApplicationController, rules defined in the Application controller automatically apply to every action in the application.

To deploy
Listing 2.17. Configuring Rails to serve static assets in production.
DemoApp::Application.configure do
  config.serve_static_assets = true

With the change in Listing 2.17, we’re now ready to push up to Heroku:

$ git commit -a -m "Serve static assets"
$ heroku create
$ git push heroku master

Finally, migrate the production database (see below if you get a deprecation warning):

$ heroku run rake db:migrate