Getting Started
RubyGems
RubyGems is a package manager for the Ruby programming language that provides a standard format for distributing Ruby programs and libraries (in a self-contained format called a "gem"), a tool designed to easily manage the installation of gems, and a server for distributing them. – https://en.wikipedia.org/wiki/RubyGems
RubyGems is a tool to install other people's code.
Update RubyGems
RubyGems come with Ruby, has a command called gem. Update it to latest version:
$> gem update --system
RubyGems Commands
$> gem -v
$> gem list
$> gem search xls
$> gem install axlsx
$> gem search rubyzip
$> gem install rubyzip
Where to find more RubyGems
Bundler
Bundler is another RubyGem. Bundler provides a consistent environment for Ruby projects by tracking and installing the exact gems and versions that are needed.
Those dependencies are listed in a file called Gemfile. Bundler will resolve dependencies automatically for you.
Run a command:
bundle install
Bundler will ensure that the gems you need are present in all environments.
Starting work on a project is as simple as bundle install
.
Ruby on Rails
Books
What is Rails?
A web framework created by David Heinemeier Hansson (known as DHH). Partner of Basecamp (formerly 37 Signals). A developer, writer, and race car driver.
Rails History
Over 10 years of Rails. 57000+ commits with 4300+ contributors and 280+ versions. Milestones:
Milestone | Date | End of Life |
---|---|---|
Open Sourced | Jul 2004 | |
Version 1.0 | Dec 2005 | |
Version 2.0 | Dec 2007 | |
Version 2.1 | Jun 2008 | |
Version 2.2 | Nov 2008 | |
Version 2.3 | Mar 2009 | Jun 2013 |
Version 3.0 | Aug 2010 | |
Version 3.1 | Aug 2011 | |
Version 3.2 | Jan 2012 | Rails 5 |
Version 4.0 | Jun 2013 | |
Version 4.1 | Apr 2014 | |
Version 4.2 | Dec 2014 |
Rails Philosophy
- Convention over Configuration
- Don't Repeat Yourself
- Agile
- MVC
Rails Architecture
- Action View
- Action Dispatcher
- Action Controller
- Active Record
- Action Mailer
- Active Job
- Active Support
Scaffold
Use rails scaffold
to create a CRUD (Create, Read, Update, Delete).
rails new shop --skip-test-unit --database=postgresql
View available options like --skip-test-unit
and --database
options by typing rails
.
- What is
--skip-test-unit
? - What is
--database
? bundle install
Rails is a framework around various libraries.
View scaffold usage by typing: rails generate scaffold
.
rails generate scaffold product name:string description:text price:integer
Then create and migrate the database:
rake db:create db:migrate
Fires up the Rails:
rails server
Open http://localhost:3000/products in your browser and click around.
We are done with the Feature!
But let's take a step back, to see what we just accomplished.
rails
is a commandgenerate
is a subcommand ofrails
, to generate files (scaffold, migration)scaffold
tellsgenerate
what to generate here- and the rest (
name:string description:text price:integer
) are the attributes of this "thing"
Scaffold is kind of CRUD. For any web applications you want to do. Identify CRUD stuff.
Examples:
- Airbnb
- Blog
- Search
What are the CRUD in these applications?
Once you have this, you are halfway there. Next would be workflows.
Deep dive into scaffold
Let’s go through what scaffold just did.
- In order to CRUD product, we need models, views, controllers and routes
- Naming Conventions (controllers vs. views vs. models vs database tables)
What we have done is to let Rails generate all these files for us.
Scaffold Magic for CRUD!
In Production quality code, we avoid using scaffold. And generate each of these files ourselves, as and when we need them!
# Model
invoke active_record
create db/migrate/20140507125949_create_products.rb
create app/models/product.rb
# Route
invoke resource_route
route resources :products
# Controller
invoke scaffold_controller
create app/controllers/products_controller.rb
# View
invoke erb
create app/views/products
create app/views/products/index.html.erb
create app/views/products/edit.html.erb
create app/views/products/show.html.erb
create app/views/products/new.html.erb
create app/views/products/_form.html.erb
invoke helper
create app/helpers/products_helper.rb
invoke jbuilder
create app/views/products/index.json.jbuilder
create app/views/products/show.json.jbuilder
invoke assets
invoke coffee
create app/assets/javascripts/products.js.coffee
invoke scss
create app/assets/stylesheets/products.css.scss
invoke scss
create app/assets/stylesheets/scaffolds.css.scss
Watch the singular and plural forms:
- Migration: db/migrate/20140507125949createproducts.rb
- Model: app/models/product.rb
- Route: resources :products
- Controller: app/controllers/products_controller.rb
- View:
- app/views/products
- app/views/products/index.html.erb
- app/views/products/edit.html.erb
- app/views/products/show.html.erb
- app/views/products/new.html.erb
- app/views/products/_form.html.erb
- Helper: app/helpers/products_helper.rb
- jbuilder:
- app/views/products/index.json.jbuilder
- app/views/products/show.json.jbuilder
- Assets:
- app/assets/javascripts/products.js.coffee
- app/assets/stylesheets/products.css.scss
Any rails command is reversible, for example to undo the rails generate scaffold
:
rails destroy scaffold product