Rails Introduction
for those who have heard a lot about it
Rails IntroductionRails Introduction
application structure
MVC
Model
 app/models/user.rb
class User < ActiveRecord::Base
end
class User < ActiveRecord::Base
attr_accessible :email, :password
validates :email, presence: true
end
class User < ActiveRecord::Base
attr_accessible :email, :password
validates :email, presence: true
has_many :blog_posts
end
Controller
app/controllers/users_controller.rb
class UsersController < ApplicationController
end
app/controllers/application_controller.rb
class UsersController < ApplicationController
def index
end
def show
end
def create
end
def edit
end
def update
end
def destroy
end
end
View
app/views/users/index.html.haml
Erb, HAML, Slim
http://html2haml.heroku.com
class UsersController < ApplicationController
def index
@users = User.all
end
end
­ @users.each do |user|
%h1= user.email
%h1= user.password
app/controllers/users_controller.rb
app/views/users/index.html.haml
assets
app/assets/javascripts
javascript, coffeescript
app/assets/stylesheets
CSS, SASS, LESS
app/assets/images
jpg, png, gif
decorators
app/decorators
gem 'draper'
class Student < ActiveRecord::Base
attr_accessible :first_name, :middle_name, :last_name
validates :email, presence: true
has_many :blog_posts
def fio
«#{first_name} #{middle_name} #{last_name}»
end 
def with_first_name(first_name)
User.find_by_first_name first_name
end
end
app/models/student.rb
decorators
app/decorators
gem 'draper'
class Student < ActiveRecord::Base
attr_accessible :first_name, :middle_name, :last_name
validates :email, presence: true
has_many :blog_posts
end
class StudentDecorator < Draper::Base
decorates :member
def fio
«#{model.first_name} #{model.middle_name} #{model.last_name}»
end 
def with_first_name(first_name)
User.find_by_first_name first_name
end
end
app/decorators/student_decorator.rb
uploaders
app/uploaders
gem 'carrierwave'
gem 'paperclip'
layouts
app/views/layouts/application.html.haml
%html
  %head
    %title SkyDance
    = stylesheet_link_tag    "bootstrap_and_overrides", :media => "all"
    = stylesheet_link_tag    "application", :media => "all"
    = javascript_include_tag "application"
    = csrf_meta_tags
  %body
    = yield
routes
config/routes.rb
Application.routes.draw do
  root :to => "welcome#index"
  match "admin" => "admins#login"
  get "schedule" => "lessons#schedule"
  resources :groups, :except => [:show, :index] do
    member do
      resources :lessons, :except => [:show, :index]
    end
  end
  resources :teachers do
    member do
      resources :photos, :except => [:show, :index, :edit] do
        collection do
          get 'admins'
        end
      end
    end
  end
end
Http-requests
● GET
● POST
● PUT
● DELETE
http-statuses
● 200
● 404
● 403
● 500
locales
КИРИЛЛИЦА
migrations
db/migrate
$ rails g migration add_sleep_to_my_life
db/migrate/add_sleep_to_my_life.rb
class AddSleepToMyLife < ActiveRecord::Migration
def change
add_column :my_lifes, :sleep, :fuck_you
add_column :table, :column_name, :column_type
end
end
db/migrate
$ rails g migration add_sleep_to_my_life
db/migrate
$ rails g migration add_sleep_to_my_life
tests
TDD — test­driven development 
http://travis­ci.org
http://coveralls.io
gem 'minitest'
tests
class UsersControllerTest < ApplicationController::TestCase
test «should get index» do
get :index
assert_response :success
end
end
test/functional/users_controller_test.rb
services
● http://travis­ci.org
● http://coveralls.io
● http://codeclimate.com
● http://github.com
thnx

Rails introduction