Ruby on Rails

An Intro for the WWU-Seminar User2User by Zweitag GmbH

Ruby

What does the ruby do on the rail?

Ruby

  • Created by: Yukihiro Matsumoto (Matz) - 1995
  • Object Oriented
  • Dynamic
  • Imperative
  • Open Source: github.com/ruby/ruby
class Student

  attr_accessor :name, :student_number, :semester

  def initialize(name, student_number, semester)
    @name = name
    @student_number = student_number
    @semester = semester
  end

  def getStudentCard
    puts "#{@name} #{ @student_number}"
    puts "Semester #{@semester}"
  end
end

s = Student.new("Rob", 12345, 5)
s.getStudentCard

Ruby Gems

90,954

3,899,752,751

Installing Gems

gem install my_very_cool_gem

Ruby on Rails

  • David Heinemeier Hansson (DHH) - 2004 (partner @ 37signals --> basecamp)
  • MVC / Web / ... Framework
  • github.com/rails/rails

The M in MVC

class User < ActiveRecord::Base
    has_secure_password
    attr_accessible :email, :password, :phone_number

    has_many :issues, dependent: :destroy

    validates :email, presence: true, uniqueness: { case_sensetive: false },
              length: { minimum: 6, maximum: 100 }
    validates :password, presence: true, length: { minimum: 12 }
end

The C in MVC

class UserController < ApplicationController
    
    def new
        @user = User.new
    end
    
    def create
        @user = User.new(params[:user])
        if @user.save
            session[:user_id] = @user.id
            redirect_to root_url, notice: "Thank's for registering"
        else
            render "new"
        end
    end
end

The V in MVC

= form_for @user do |f|
    - if @user.errors.any?
        .error_messages
            %h4 some errors occured
            %ul
                - @user.errors.full_messages.each do |message|
                    %li= message
    .field
        = f.label :email
        = f.text_field :email
    .field
        = f.label :password
        = f.password_field :password
    .actions
        = f.submit "Register"

Demo
Time
It is!

ruby on rails - an intro

By johannesboyne

ruby on rails - an intro

  • 1,034
Loading comments...

More from johannesboyne