This log is strongly influenced by CodeSchool Rails cheatsheet.

Rails Working Process (Application Layer)

The browser sends request which received by a web server and passed on to a Rails controller action (action handles the request). Controller then interacts with a model (Ruby object) which communicate with the database. After invoking the model, controller renders the view.

Rails Development Cheatsheets

Rails Command Line

All commands can be run with -h for more information.

Undoing Things in Rails

# New app
rails new <app name>
# Rails development server
rails s
# Rails console
rails c
# Rails db console (dbconsole)
rails db
# Rails generate template code
rails g
rails g model <name> [columnName:type]
rails g controller <name> [action_name]
rails g scaffold <name> [columnName:type]

Undoing things in Rails

# Destroy Model
rails d model <name>
rails d controller <name>

Migration

Active Record supported types,

:primary_key, :string, :text, :integer, :float, :decimal, :datetime, :timestamp, :time, :date, :binary, :boolean

Migration methods Doc,

create_table, change_table, drop_table, add_column, change_column, rename_column, remove_column, add_index, remove_index

Column options,

default:<value>, limit:30, null:false, first:true, after: :email, unique: true

# Run missing migration
rake db:migrate
# Rollback the previous migration
rake db:rollback
# Create db, load schema, seed
rake db:setup
# Dump curreht db state -> db/schema.rb
rake db:schema:dump
# Create a blank migration
rails g migration <name>
# To add column
rails g migration Add<Anything>To<TableName> [columnName:type]
# To remove column
rails g migration Remove<Anything>From<TableName> [columnName:type]

Add column,

rails g migration AddEmailToUsers email:string
class AddEmailToUsers < ActiveRecord::Migration
  def up
    add_column :users, :email, :string
  end
  def down
    remove_column :users, :email
  end
end

Remove column,

rails g migration RemoveAgeFromUsers age:integer
class RemoveAgeFromUsers < ActiveRecord::Migration
  def up
    remove_column :users, :age
  end
  def down
    add_column :users, :age, :integer
  end
end

Create table,

rails g migration CreateUsersTable name:string bio:text age:integer
class CreateUsersTable < ActiveRecord::Migration
  def up
    create_table :users do |t|
      t.string :name
      t.text :bio
      t.integer :age
      t.timestamps
    end
  end
  def down
    drop_table :users
  end
end
# Alternatively
class CreateUsersTable < ActiveRecord::Migration
  def change
    create_table :users do |t|
      t.string :name
      t.string :description

      t.timestamps
    end
  end
end

Controllers

Name controller with a plural none (Users) because it manages number of models.

Actions inside controller in preferred order,

index, show, new, create, edit, update, destroy

Models

Name model with a singular none because it refers to its type.

REST

REST (REpresentational State Transfer) architecture is for developing distributed, networked systems and software application.

Routes

Custom Routes

Forms

View Partial & View Helpers

Mailer

Assets & Asset Paths

Sprockets

Rendering & HTTP Status Codes

JSON Response

AJAX

Dependency Management

Gemfile is used to manage project dependency, detailed usage of Bundler and Gemfile is coverd in #0001 Ruby Dependency Management.

# Install the latest version
gem 'gem-name'            
# Install version newer than 3.0
gem 'gem-name', '>=3.0.0'
# Install newer than 3.0 and not newer than 3.1
gem 'gem-name', '~>3.0.0'
  • gem list <gem-name>, show gem(s) version list
  • bundle install or bundle, install gem(s) in Gemfile
  • bundle update, updates gems

Rails Project Development Process

The following process is for development in BDD(behavior-driven development) style. (Big Fan!!!)

BDD is an extension of TDD that use of DSL. 1

  1. Write a feature test
  2. Run test and check error
  3. Implement feature or fix error
  4. test pass ? go to step (5) : go to step (2)
  5. Write next feature text
  1. https://en.wikipedia.org/wiki/Behavior-driven_development