Rails introduction

download Rails introduction

If you can't read please download the document

Transcript of Rails introduction

Rails Introduction

for those who have heard a lot about it

Rails Introduction

Rails Introduction

application structure

MVC

Model

app/models/user.rb

class User < ActiveRecord::Baseend

class User < ActiveRecord::Baseattr_accessible :email, :password

validates :email, presence: trueend

class User < ActiveRecord::Baseattr_accessible :email, :password

validates :email, presence: true

has_many :blog_postsend

Controller

app/controllers/users_controller.rb

class UsersController < ApplicationControllerend

app/controllers/application_controller.rb

class UsersController < ApplicationControllerdef indexenddef showenddef createenddef editenddef updateenddef destroyendend

View

app/views/users/index.html.hamlErb, HAML, Slimhttp://html2haml.heroku.com

class UsersController < ApplicationControllerdef index@users = User.allendend

- @users.each do |user|%h1= user.email%h1= user.password

app/controllers/users_controller.rb

app/views/users/index.html.haml

assets

app/assets/javascriptsjavascript, coffeescript

app/assets/stylesheetsCSS, SASS, LESS

app/assets/imagesjpg, png, gif

decorators

app/decoratorsgem 'draper'

class Student < ActiveRecord::Baseattr_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_nameendend

app/models/student.rb

decorators

app/decoratorsgem 'draper'

class Student < ActiveRecord::Baseattr_accessible :first_name, :middle_name, :last_name

validates :email, presence: true

has_many :blog_posts

end

class StudentDecorator < Draper::Basedecorates :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_nameendend

app/decorators/student_decorator.rb

uploaders

app/uploadersgem '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 endend

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::Migrationdef changeadd_column :my_lifes, :sleep, :fuck_youadd_column :table, :column_name, :column_typeendend

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.orghttp://coveralls.iogem 'minitest'

tests

class UsersControllerTest < ApplicationController::TestCasetest should get index doget :indexassert_response :successendend

test/functional/users_controller_test.rb

services

http://travis-ci.org

http://coveralls.io

http://codeclimate.com

http://github.com

thnx