Backend with Ruby on Rails

guillermo{at}guerreroibarra{dot}com

Follow me on github: https://github.com/ryanfox1985

May 25th, 2016

What is it?

  • Initial release: 18 December 2005
  • Stable release: 4.2.6
  • Convention over configuration (CoC)
  • Don't repeat yourself (DRY)
  • Design pattern: MVC

MVC

RoR Controllers

  • config/routes.rb
  • app/controllers
  • Application controller (Parent class)

RoR Models

  • app/models

RoR Views

  • app/views/layouts
  • app/views
  • Individual folder for each controller

Rails cli

                    
$ rails --help
$ rails new blog
$ cd blog; rails s
$ cd blog; rails c
                    
                

Rake cli

  • rake
  • rake routes
  • rake db:create | rake db:drop
  • rake db:migrate | rake db:rollback
  • rake db:seed
  • rake db:schema:load
  • rake (lib_task)

RoR project folders:

                
|____app
| |____assets
| |____controllers
| |____helpers
| |____mailers
| |____models
| |____views
|____bin
|____config
|____db
|____Gemfile
|____lib
|____log
|____public
|____test
|____tmp
|____vendor
                
            

Dependencies (Gemfile)

Rubygems and Bundler cli:

  • bundle install
  • bundle update

Environments

Database configurations, application behaviours, code inline

  • Development
  • Test
  • Production

Scaffolding

                
$ rails g scaffold post title:string content:text
$ rails g scaffold comment post:references author:string opinion:text
                
            
  • rake db:migrate

Seeding

                
# db/seeds.rb

10.times do
  p = Post.create(title: Faker::Hipster.sentence, content:Faker::Hipster.paragraph)

  5.times do
    Comment.create(post: p, author: Faker::App.author, opinion: Faker::Hipster.sentence)
  end
end
                
            
  • bundle install

API REST

Testing

  • mini-test
  • test/*
  • test/fixtures
  • test/test_helper.rb

Coverage

                
#test/test_helper.

require 'simplecov'
SimpleCov.start
                
            
  • bundle install

Questions?