Slide 1

Slide 1 text

Rails Girls Frankfurt am Main 2013 A BRIEF OVERVIEW OF RUBY ON RAILS

Slide 2

Slide 2 text

WHO AM I? Nicolai Reuschling [email protected] software developer, agile coach & lecturer

Slide 3

Slide 3 text

„MATZ“ Yukihiro Matsumoto inventor, designer & creator of Ruby

Slide 4

Slide 4 text

„DHH“ David Heinemeier Hansson creator of Ruby on Rails

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

RUBY ON RAILS FRAMEWORK implemented in Ruby collection of great solutions for common problems highly opinionated golden path leads to high productivity configurable as well (convention over configuration)

Slide 11

Slide 11 text

PROJECT SCAFFOLD Run in console: rails new rails-girls-weblog-demo

Slide 12

Slide 12 text

SCAFFOLDING code generator tool generates all necessary files model, views, controller routing configuration database migration great and easy way to start and build upon Run in console (one line): rails generate scaffold Post title:string body:text

Slide 13

Slide 13 text

PROJECT SCAFFOLD

Slide 14

Slide 14 text

PROJECT SCAFFOLD

Slide 15

Slide 15 text

PROJECT SCAFFOLD

Slide 16

Slide 16 text

PROJECT SCAFFOLD

Slide 17

Slide 17 text

BIRD‘S EYE VIEW View Assets + Internet Routing Controller Model Database MVC architecture

Slide 18

Slide 18 text

BIRD‘S EYE VIEW View Assets + Internet Routing Controller Model Database MVC architecture

Slide 19

Slide 19 text

BIRD‘S EYE VIEW View Assets + Internet Routing Controller Model Database MVC architecture

Slide 20

Slide 20 text

ROUTING too magical and complex to explain in this context basically, it decides: what request goes where? splits URI and forwards request to controller, e.g.: /posts/ goes to PostsController /comments/42 goes to CommentsController let‘s assume: it just works!

Slide 21

Slide 21 text

BIRD‘S EYE VIEW View Assets + Internet Routing Controller Model Database MVC architecture

Slide 22

Slide 22 text

BIRD‘S EYE VIEW View Assets + Internet Routing Controller Model Database MVC architecture

Slide 23

Slide 23 text

CONTROLLER handles HTTP requests and sessions collaborates with models pushes information into the views decides, which view and what format to render

Slide 24

Slide 24 text

CONTROLLER ACTIONS index (collection; list of all available resources) show (single resource; displays values) new (form for creating a new resource) create (logic to create a new resource) edit (form for updating an existing resource) update (logic to update an existing resource) destroy (logic to delete an existing resource)

Slide 25

Slide 25 text

CONTROLLER EXAMPLE 1/2 # GET /posts # GET /posts.json # GET /posts.xml def index @posts = Post.all respond_to do |format| format.html # index.html.erb format.json { render json: @posts } format.xml { render xml: @posts } end end

Slide 26

Slide 26 text

CONTROLLER EXAMPLE 2/2 # GET /posts/1 # GET /posts/1.json # GET /posts/1.xml def show @post = Post.find(params[:id]) respond_to do |format| format.html # show.html.erb format.json { render json: @post } format.xml { render xml: @post } end end

Slide 27

Slide 27 text

CONTROLLER EXAMPLE 2/2 # GET /posts/1 # GET /posts/1.json # GET /posts/1.xml def show @post = Post.find(params[:id]) respond_to do |format| format.html # show.html.erb format.json { render json: @post } format.xml { render xml: @post } end end

Slide 28

Slide 28 text

CONTROLLER EXAMPLE 2/2 # GET /posts/1 # GET /posts/1.json # GET /posts/1.xml def show @post = Post.find(params[:id]) respond_to do |format| format.html # show.html.erb format.json { render json: @post } format.xml { render xml: @post } end end

Slide 29

Slide 29 text

BIRD‘S EYE VIEW View Assets + Internet Routing Controller Model Database MVC architecture

Slide 30

Slide 30 text

BIRD‘S EYE VIEW View Assets + Internet Routing Controller Model Database MVC architecture

Slide 31

Slide 31 text

MODELS implements „business logic“ (your application idea) associates collaborators and connects data validates information stores and retrieves information from the database

Slide 32

Slide 32 text

MODEL EXAMPLE 1/2 class Post < ActiveRecord::Base # attributes attr_accessible :body, :title # associations has_many :comments # validations validates :title, presence: true, uniqueness: true, length: { within: 2..100 } validates :body, length: { minimum: 10 } # scopes default_scope order('created_at DESC') end

Slide 33

Slide 33 text

MODEL EXAMPLE 2/2 class Comment < ActiveRecord::Base # attributes attr_accessible :author_name, :body, :post_id # associations belongs_to :post # validations validates :author_name, presence: true, length: { maximum: 100 } validates :body, length: { within: 10..1000 } validates :post, associated: true validates :post_id, presence: true # scopes default_scope order('created_at ASC') end

Slide 34

Slide 34 text

BIRD‘S EYE VIEW View Assets + Internet Routing Controller Model Database MVC architecture

Slide 35

Slide 35 text

BIRD‘S EYE VIEW View Assets + Internet Routing Controller Model Database MVC architecture

Slide 36

Slide 36 text

MIGRATIONS configuration tool for databases creates/deletes or changes tables may be used to change large sets of data

Slide 37

Slide 37 text

MIGRATION EXAMPLE class CreatePosts < ActiveRecord::Migration def change create_table :posts do |t| t.string :title t.text :body t.timestamps end end end Run in console: rake db:migrate

Slide 38

Slide 38 text

BIRD‘S EYE VIEW View Assets + Internet Routing Controller Model Database MVC architecture

Slide 39

Slide 39 text

BIRD‘S EYE VIEW View Assets + Internet Routing Controller Model Database MVC architecture

Slide 40

Slide 40 text

BIRD‘S EYE VIEW View Assets + Internet Routing Controller Model Database MVC architecture

Slide 41

Slide 41 text

BIRD‘S EYE VIEW View Assets + Internet Routing Controller Model Database MVC architecture

Slide 42

Slide 42 text

VIEWS ready HTML page = layout + template + partials mixture of HTML tags plus embedded Ruby (ERB) access to all instance variables (@var) set by controller other formats (e.g. JSON, XML) possible as well rendered view is delivered to browser

Slide 43

Slide 43 text

VIEW EXAMPLE (INDEX)

My Weblog

<% @posts.each do |post| %> <% end %> Title Body Actions <%= post.title %> <%= truncate(post.body, separator: ' ', omission: '… (continued)') %> <%= link_to 'Show', post %> <%= link_to 'Edit', edit_post_path(post) %> <%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' } %>

Slide 44

Slide 44 text

VIEW EXAMPLE (SHOW)

<%= notice %>

<%= @post.title %>

<%= @post.body %>


Comments

<% @post.comments.each do |comment| %>

<%= comment.author_name %> commented on <%= comment.created_at.to_s(:long) %>

<%= comment.body %>

<% end %>

Slide 45

Slide 45 text

BIRD‘S EYE VIEW View Assets + Internet Routing Controller Model Database MVC architecture

Slide 46

Slide 46 text

BIRD‘S EYE VIEW View Assets + Internet Routing Controller Model Database Javascript, stylesheet, images MVC architecture

Slide 47

Slide 47 text

BIRD‘S EYE VIEW View Assets + Internet Routing Controller Model Database Javascript, stylesheet, images MVC architecture

Slide 48

Slide 48 text

BIRD‘S EYE VIEW View Assets + Internet Routing Controller Model Database Javascript, stylesheet, images MVC architecture

Slide 49

Slide 49 text

FEED YOUR BRAIN 1/2 Ruby http://ruby-doc.org/ Ruby on Rails API (Official Code Documentation) http://api.rubyonrails.org/ Ruby on Rails Guides http://guides.rubyonrails.org/

Slide 50

Slide 50 text

FEED YOUR BRAIN 2/2 Screencasts http://railscasts.com/ The Pragmatic Bookshelf http://pragprog.com/categories/ruby_and_rails Demo Project https://github.com/ncreuschling/rails-girls-weblog-demo

Slide 51

Slide 51 text

QUESTIONS? ASK THEM! OTHERWISE: HAVE FUN!

Slide 52

Slide 52 text

QUESTIONS? ASK THEM! OTHERWISE: HAVE FUN! Thanks!

Slide 53

Slide 53 text

CITATIONS/SOURCES title photo by Jaroen Kransen http://www.flickr.com/photos/jkransen/5231797723/ Matz http://de.wikipedia.org/wiki/Yukihiro_Matsumoto DDH http://david.heinemeierhansson.com/