+ All Categories
Home > Technology > 10 Reasons Why I Love Ruby On Rails

10 Reasons Why I Love Ruby On Rails

Date post: 20-Jun-2015
Category:
Upload: punneng
View: 3,369 times
Download: 3 times
Share this document with a friend
Popular Tags:
34
  10 reasons why I love Ruby on Rails Surasit Liangpornrattana a.k.a. PunNeng
Transcript
Page 1: 10 Reasons Why I Love Ruby On Rails

  

10 reasons why I love Ruby on Rails

Surasit Liangpornrattanaa.k.a. PunNeng

Page 2: 10 Reasons Why I Love Ruby On Rails

  

Ruby on Rails

rubyonrails.org an open-source web framework that's

optimized for programmer happiness and sustainable productivity. It lets you write beautiful code by favoring convention over configuration

David Heinemeier Hansson - DHH

Page 3: 10 Reasons Why I Love Ruby On Rails

  

1. Ruby

Ruby Lin rubystation.com

Page 4: 10 Reasons Why I Love Ruby On Rails

  

1. Ruby

ruby-lang.org Yukihiro “matz” Matsumoto Matz wrote “Treating code as an essay” in

“Beautiful code” Easy to read/understand Increase productivity

RubyGems

Page 5: 10 Reasons Why I Love Ruby On Rails

  

Treating code as an essay

Rubyputs "Hello World"

Javaclass MyJavaProg{ public static void main(String args[]){ System.out.println("Hello World"); }}

Page 6: 10 Reasons Why I Love Ruby On Rails

  

RubyGems

http://rubyforge.org $ gem install rails $ gem install ????

Page 7: 10 Reasons Why I Love Ruby On Rails

  

2. Philosophy and Design

Convention over Configuration(CoC) Don't repeat yourself(DRY)

Page 8: 10 Reasons Why I Love Ruby On Rails

  

Convention over Configuration

Table name users

Model user.rb class User < ActiveRecord::Base; end

Restful controller users_controller.rb class UsersController < ApplicationController; end

View app/views/users/*.html.erb

Page 9: 10 Reasons Why I Love Ruby On Rails

  

DRY – Don't repeat yourself

Controllerclass UsersController < ApplicationController def create logger.info "Before create" # ... end def update logger.info "Before update" # ... endend

Page 10: 10 Reasons Why I Love Ruby On Rails

  

DRY – Don't repeat yourself

before_filter :show_log, :only => [ :create, :update]

def create # ... end def update # ... end protected def show_log logger.info "Before #{params[:action]}" end

Page 11: 10 Reasons Why I Love Ruby On Rails

  

DRY – Don't repeat yourself

Model - Callbacks before_create after_create before_save before_save etc. API

Page 12: 10 Reasons Why I Love Ruby On Rails

  

3. MVC

M – Model ActiveRecord

V – View ActionView

C – Controller ActionController

Page 13: 10 Reasons Why I Love Ruby On Rails

  

Controller

# app/controllers/users_controller.rbclass UsersController < ApplicationController def index @users = User.find :all end

def new @user = User.new end

def create @user = User.new params[:user] if @user.save redirect_to :action => "index" else render :action =>"new" end endend

Page 14: 10 Reasons Why I Love Ruby On Rails

  

View

# app/views/users/index.html.erb<h2>User list</h2><%= @users.each do |user| %> <p>Name: <%= user.name %></p> <p>Age: <%= user.age %></p> <hr /><% end %>

# partial rendering<h2>User list</h2><%= render :partial "user_list", :collection => @users

# app/views/users/_user_list.html.erb<%= collection.each do |user| %> <p>Name: <%= user.name %></p> <p>Age: <%= user.age %></p> <hr /><% end %>

Page 15: 10 Reasons Why I Love Ruby On Rails

  

4. ActiveRecord

user = User.new :name => "Neng", :age => 25user.save #=> true

User.find 1neng = User.find_by_name "Neng"# SELECT * FROM `users` WHERE (`name` = 'Neng') LIMIT 1neng.age #=> 25

Page 16: 10 Reasons Why I Love Ruby On Rails

  

ActiveRecord::Migration

# db/migrate/001_create_users.rbclass CreateUsers < ActiveRecord::Migration def self.up create_table :users do |t| t.string :name, :null => false t.integer :age, :status end

User.create :name => "Neng", :age => 25 end

def self.down drop_table :users endend

Page 17: 10 Reasons Why I Love Ruby On Rails

  

ActiveRecord::Validations

# app/models/user.rbclass User < ActiveRecord::Base validates_presence_of :name validates_uniqueness_of :name validates_length_of :name, :in => 5..30end

user = User.newuser.save #=> falseuser.errors.full_messages #=> ["Name is too short (minimum is 5 characters)", "Name can't be blank"]

Page 18: 10 Reasons Why I Love Ruby On Rails

  

ActiveRecord::Callbacks

# app/models/user.rbclass User < ActiveRecord::Base validates_presence_of :name validates_length_of :name, :in => 5..30

before_save :set_status

def set_status status = 1 endend

user = User.new :name => "Neng"user.save #=> falseuser.status #=> 1user.set_status #=> 1

Page 19: 10 Reasons Why I Love Ruby On Rails

  

5. Ajax helpers

# It is PrototypeJS!!# Generates: <a href="#" onclick="new# Ajax.Updater({success:'posts',failure:'error'}, '/blog/destroy/5',# {asynchronous:true, evalScripts:true}); return false;">Delete this post</a> link_to_remote "Delete this post", :url => { :action => "destroy", :id => post.id }, :update => { :success => "posts", :failure => "error" }

# Generates:# <form action="/" method="post" onsubmit="new Ajax.Request('/',# {asynchronous:true, evalScripts:true, parameters:Form.serialize(this)});# return false;"><div><input name="commit" type="submit"value="Save" /></div># </form> <% form_remote_tag :url => '/posts' do -%> <div><%= submit_tag 'Save' %></div> <% end -%>

Page 20: 10 Reasons Why I Love Ruby On Rails

  

Ajax helpers

# Generates: new # Form.Element.Observer('suggest', 0.25, function(element, value) {new# Ajax.Updater('suggest',# '/testing/find_suggestion', {asynchronous:true, evalScripts:true, parameters:'q='# + value})}) <%= observe_field :suggest, :url => { :action => :find_suggestion }, :frequency => 0.25, :update => :suggest, :with => 'q' %>

# Generates:# new PeriodicalExecuter(function() {new Ajax.Updater('news_block', 'update',# {asynchronous:true, evalScripts:true})}, 20) periodically_call_remote(:url => 'update', :frequency => '20', :update => 'news_block')

Page 21: 10 Reasons Why I Love Ruby On Rails

  

Ajax helpers

# app/controllers/users_controller.rbclass UsersController < ApplicationController def index @users = User.find :all # request.xhr? respond_to do |format| format.html format.js do # render :text => "replaced message" # or RJS render :update do |page| page.insert_html :bottom, 'list', "<li>#{@users.last.name}</li>" page.visual_effect :highlight, 'list' page.hide 'status-indicator', 'cancel-link' end end end endend

Page 22: 10 Reasons Why I Love Ruby On Rails

  

6. Restful

REST - Representational state transfer Style of software architecture for distributed

hypermedia systems such as the World Wide Web

Roy Fielding - One of the principal authors of HTTP specification

Outline how resources are defined and addressed

Web services Main feature in Rails 2.0

Page 23: 10 Reasons Why I Love Ruby On Rails

  

7. Cool plugins

HAML ActiveScaffold MakeResourceful File column/Upload column Restful authentication Acts as tree, versioned, etc.

Page 24: 10 Reasons Why I Love Ruby On Rails

  

8. Capistrano

capify.org Automated Deployment System Version control system – svn / git $ svn commit / $ git push $ cap deploy

Page 25: 10 Reasons Why I Love Ruby On Rails

  

$ svn commit / git push

PCReposvn/git

Host

Upload every changes to git/svn repo

Page 26: 10 Reasons Why I Love Ruby On Rails

  

$ cap deploy

PCReposvn/git

Host

Upload every changes to git/svn repo

Remote to host and run $ svn update / git pull

Page 27: 10 Reasons Why I Love Ruby On Rails

  

$ cap deploy

PCReposvn/git

Host

Upload every changes to git/svn repo

Remote to host and run $ svn update / git pull

$ svn update / git pull

Page 28: 10 Reasons Why I Love Ruby On Rails

  

9. RSpec

rspec.info Behavior Driven Development – BDD Story framework Spec framework

Page 29: 10 Reasons Why I Love Ruby On Rails

  

BDD

Encourages collaboration between developers, QA and non-technical or business participants in a software project

Test First

Page 32: 10 Reasons Why I Love Ruby On Rails

  

10. FREE !!

Open Source

Page 33: 10 Reasons Why I Love Ruby On Rails

  

Q & A

Page 34: 10 Reasons Why I Love Ruby On Rails

  

Bye


Recommended