6ftDan(TM) Rails Primer & Cheat Sheet

>> This is currently a fluctuating DRAFT VERSION <<


I recommend Bitnami’s all-in-one installer for the Ruby stack: https://bitnami.com/stack/ruby

Getting Started

echo 'generate folder and bare-bone project'
rails new MyProjectName

echo 'enter project directory'
cd MyProjectName

echo 'run a server on your local machine'
rails server

echo 'open your web browser to localhost:3000'
firefox localhost:3000

If you’ve never used, or tried, Rails before consider these steps as mandatory!  Rails isn’t a start from scratch system.  You get a basic “engine” with a bit of structure on which you work with.  If you want to build something entirely from scratch I’d recommend Sinatra as it’s really bare bones.

At this point you have a running Rails website and you can see it working when your browser is navigated to localhost:3000 .  You will want to check your updates as you go along in the browser to see if you break anything.

Heads Up Clue: To change what the first page loads as you will change your config/routes.rb file to whatever view you set up in the view folder.

The Layout

Most of what you build will entirely be in the sub-folder ‘app/’.


These are the core folders you will be using for the project.


Just like the project structure was built with rails new, so also when you want to add a component of any kind there are generators to build the basic template structure of each part.  These are some of generator commands available:


At any time you can tack –help on the rails command to get more details.  You can get pretty specific with it to.  From rails –help to rails generate model –help .  This will give you a howto and detail on each usage and will help you get started the fastest.

The “all in one” generator is the scaffold generator.  When you’re generating a resource it typically goes by COMMAND RESOURCE FEILD1:TYPE FIELD2:TYPE etc.  For example:

rails generate scaffold Posts story:text subject:string:index user:references

Here user:references will do all the work of creating a belongs_to relationship to the User object.  You will still need to open up the app/models/users.rb file and add a line has_many :posts for the User object instances to refer back to the posts they own.  You don’t need to fully understand this yet, just know that when you have one object belongs_to :another object, the other object should say whether it has_one :object or has_many :objects.

Back to generators.  The scaffold generator builds the basic files for a view, controller, model, and helper.  These are each in their appropriately named folders under app/

If you don’t want to build the database model but you do want to build all the other application view/controller logic then you can use the scaffold_controller generator command instead.


The default database in a generated project is Sqlite3 which doesn’t require system configuration to use.  After generating your model you will need to update the database before the rails server will work.  I recommend looking at the models you’ve generated in the db/migrate/ folder and make sure everything is as you would like it to be before updating your database tables.  Things like null: false, unique: true, and default: “value”.  Also if you didn’t specify as many indexes as you need you can add them near the end outside the create table block.

add_index :books, :publisher_name
add_index :books, :author
add_index :books, :name
add_index :books, [:author, :name]

Once you’ve verified your migrations are ready to be added to the database you can update the database from the command line.

echo 'if you haven't created the database yet use rake db:create'
rake db:create

echo 'update all your new migrations into the database'
rake db:migrate

echo 'forgot something? roll back as many migrations as you need'
rake db:rollback STEP=1

echo 'want to scrap the DB? drop the whole database'
rake db:drop


The generators will build basic CRUD (Create Read Update Destroy/(Delete)) routes in your config/routs.rb file for each resource.  Example from Rails documentation:

HTTP Verb Path Controller#Action Used for
GET /photos photos#index display a list of all photos
GET /photos/new photos#new return an HTML form for creating a new photo
POST /photos photos#create create a new photo
GET /photos/:id photos#show display a specific photo
GET /photos/:id/edit photos#edit return an HTML form for editing a photo
PATCH/PUT /photos/:id photos#update update a specific photo
DELETE /photos/:id photos#destroy delete a specific photo

Generally it’s easiest to use the resources in the way they were intended to be used.  As you develop more experience customizing the resources will be more feasible.

Routing is configured in the config/routes.rb file.  The defacto way to use the resource routes is with the variable resources.

resources :contacts

The routing has been redesigned many time since Rails began so there are many ways to use it.

resources :users do
  collection do
    get 'check_valid_phone'
    get 'check_email_registered'

get '/auth/:provider/callback' => 'omni_auth#callback'

match "/invite/:id", to: "invite#destroy", via: [:delete], as: 'destroy_invite'

resources :dashboards, only: [:index]
resources :events do
  collection do
    patch 'event_ids'
match "/events/:id/join", to: "events#join", as: "join_event", via: :get
match '/inbox/', to: "inbox#index", via: [:get], as: 'inbox'
match '/email/send', to: 'emails#send_email', via: [:patch], as: 'send_email'
match '/email/autocomplete', to: 'emails#autocomplete', via: [:get], as: 'email_autocomplete'
resources :emails, path: 'email', only: [:show], as: 'email' do
  collection do
    patch 'email_ids'
resources :tags, only: [:update]
resources :feedbacks, path: 'feedback', except: [:show, :new, :edit]

unauthenticated do
  root to: "visitors#index", as: :unauthenticated_root
authenticated :user do
  root to: "dashboards#index", as: :authenticated_root

authenticated :user, lambda {|u| !!AdminUser.where(email: u.email).first } do
  mount Resque::Server, :at => "/admin/resque"
  scope :admin do
    devise_for :admin_users, ActiveAdmin::Devise.config
get "/admin" => redirect("/")

namespace :api, defaults: { format: :json } do
  namespace :v1 do
    match 'social/:username/followers' => 'social#followers', via: :get

scope :ujs, defaults: { format: :ujs } do
  patch 'invite' => 'ujs#invite'

In the routes when it routes “to” something it goes by “controller#method“.


When a route is loaded it loads the controller that matches first.  The controller is responsible for whatever objects that need to be instantiated as variables for the view.  When a controller method is loaded it will either need to have an appropriate HTTP response, or if left without one it Rails will load a page by the same name in the views directory.  If neither are provided you will have an error on the website.

>> This is currently a fluctuating DRAFT VERSION <<

Leave a Reply

Your email address will not be published / Required fields are marked *