Content:
- Logic
- Introduction
- Conventions
Ruby on Rails Logic
*Browser* <--request/response--> *Server* --request_info--> *Routing*
^ |
| Params + Methods
| |
HTML + Assets V
| --- *Controller* <---
| | |
| Data Data
| | |
| V V
|________________ *View* *Model* <---ORM---> *DB*
\- app
\- assets #(CSS, JS dependencies)
\- model #\
\- view #=(MVC structure)
\- controller #/
\- config
\= routes.rb #(define routes)
\- db
\- migration #(migration files for models)
\= schema.rb #(database schema)
\= Gemfile #(third party libraries)
Rails
- HTTP method: GET, POST, PATCH, DELETE
- URL Pattern
- Literals: “/posts”, “/home”, “/us/about-us”
- Pattern: “/posts/:id”, “/plus/:first/:second”
- Wildcard Values
- Handler Method: ClassName#method_name
A Web Framework
- parses request to params
Route: POST, /create-post, PostsController#create
Data: title, body as json
Handler
Database
- Create, read, update, delete records
- database schema
rails new demo
rails g model Post title:string body:text
# rails g model ModelName attr_name: attr_type
rails g model Post title:string body:text
rails db:migrate
rails c # pull up an irb environment
Conventions:
Ruby Conventions
Class names are CamelCase
Methods and variables are snake_case
Methods with a ?
suffix returns a boolean.
Methods with a !
suffix
- either operates destructively
- or it will raise and exception instead of failing (such as Rails models
#save!
vs.#save
).
In documentation, ::method_name
denotes a class method, while #method_name
denotes a instance method.
Database
Tables use snake_case
Names are plural
+--------------------------+
| bigfoot_sightings |
+------------+-------------+
| id | ID |
| sighted_at | DATETIME |
| location | STRING |
| profile_id | FOREIGN KEY |
+------------+-------------+
Model
Model class names use CamelCase
and is singular. Names will map to the plural database table name.
Attributes and methods use snake_case
and match column names in the database.
Model files in app/models/#{singular_model_name}.rb
# app/models/profile.rb
class Profile < ActiveRecord::Base
# Methods follow the same conventions as attributes
def veteran_hunter?
years_of_experience > 2
end
end
Relations in models
Use snake_case
. has_one and belongs_to are singular while has_many is plural.
Foreign key have an _id suffix
.
# app/models/bigfoot_sighting.rb
class BigfootSighting < ActiveRecord::Base
# This knows to use the profile_id field in the database
belongs_to :profile
end
Controllers
Controller class names use CamelCase
and have Controller
as a suffix (always singular).
The name of the resource is plural.
Controller actions use snake_case
and match the route names (index, show, new, create, edit, update, delete).
Controller files in app/controllers/#{resource_name}_controller.rb
# app/controllers/bigfoot_sightings_controller.rb
BigfootSightingsController < ApplicationController
def index
# ...
end
def show
# ...
end
# etc
end
Routes
Routs name are snake_case
and usually match the controller.
Routes are usually plural and use resources
resources :bigfoot_sightings
# Users can only see their own profiles, so we'll use `/profile` instead
# of putting an id in the URL.
resource :profile
Views
View files match the corresponding controller and action.
app/views/#{resource_name}/#{action_name}.html.erb
app/views/bigfoot_sightings/index.html.erb