SlideShare a Scribd company logo
1 of 11
Intro to Rails
What do we look for in a framework?
• Please don’t suck
– Rails does not suck
• Does it follow Model-View-Controller?
– Yes
– Since Rails 1 it’s been the standard bearer for how to do MVC on the web, copied in almost every language
• Does it help me avoid repeating myself (DRY)?
– Yes
• Is it self documenting?
– Yes, it has a set of rules that generally make most documentation unnecessary
• Is it flexible enough to bend to my application needs?
– Yes
• Do other people use it?
– Good gosh yes
• Will it work with my database?
– Yes
• Is it still going to be around in X years?
– Ruby has Rails
– Python has Django
– Groovy has Grails
– C# has MVC
– PHP has fragmented framework Hell (aka – who knows?)
– Java has a few major players (Struts 2, Play, etc)
Request Flow
Rack
Watch this excellent walkthrough of Rack
Middleware:
http://railscasts.com/episodes/151-rack-
middleware
Summary:
It’s a layer of ruby code that passes requests into your app and
sends responses back out. You can add layers to do pre/post
processing on all requests prior to beginning ANY of your
application code.
Before the Request
• Configuration
• Initializers
• Gems
• Environments
• Asset Pipeline
Models / ActiveRecord
class Post < ActiveRecord::Base
belongs_to :category
has_many :tags, through: :posts_tags
validates :title, presence: true
before_save :create_slug, only: :create
scope :newest_first, order(‘created_at DESC’)
scope :active, where(‘active = ?’,true)
scope :newest_active, newest_first.active
scope :search, lambda do |text|
where(‘title LIKE ?’,”%#{text}%”)
end
def create_slug
self.slug = title.downcase.squish.sub(‘ ‘,’-’)
end
end
post = Post.new(title: ‘Some title’)
post.save!
OR
post = Post.create(title: ‘Some title’)
post.slug # some-title
post.id # 1
post.created_at # Created datetime
post.updated_at # Updated datetime
post.title = ‘New title’
post.save!
# Relations
post.tags.first
post.tags.count
post.category.name
post = Post.include(:tags) # Eager load
post =
Post.search(‘some’).newest_active.first
Migrations
class CreateInitialTables < ActiveRecord::Migration
def up
create_table :posts do |t|
t.string :title
t.text :body
t.string :slug
t.integer :category_id
t.timestamps
end
# … create more tables…
add_index :tags, [:name,:something], unique: true
execute “UPDATE posts SET field = ‘value’ WHERE stuff = ‘happens’”
end
def down
drop_table :posts
end
def change
add_column :posts, :user_id, :integer
end
end
$ rake db:migrate
Controllers and REST
Class PostsController < ApplicationController
before_filter :authenticate, only: :destroy
def index # GET /posts
end
def new # GET /posts/new
end
def create # POST /posts
end
def show # GET /posts/:id
end
def edit # GET /posts/:id/edit
end
def update # PUT /posts/:id
end
def destroy # DELETE /posts/:id
end
end
# Routes
resources :posts
OR limit it
resources :posts, only: [:create,:new]
Views
/app/views
/layouts
/application.html.erb
/posts
/new.html.slim
/new.json.rabl
/index.xml.erb
/_widget.html.erb
# slim example
.post
h2=post.title
.body.grid-8=post.body
# erb example
<div class=“post”>
<h2><%=post.title%></h2>
<div class=“body grid-8”>
<%=post.body%>
</div>
</div>
Generators
• rails new .
• rails g scaffold article name content:text
published_on:date
LET’S WRITE SOME CODE!

More Related Content

What's hot

Content Management With Apache Jackrabbit
Content Management With Apache JackrabbitContent Management With Apache Jackrabbit
Content Management With Apache Jackrabbit
Jukka Zitting
 

What's hot (20)

WordPress APIs
WordPress APIsWordPress APIs
WordPress APIs
 
Building Apis in Scala with Playframework2
Building Apis in Scala with Playframework2Building Apis in Scala with Playframework2
Building Apis in Scala with Playframework2
 
Develop realtime web with Scala and Xitrum
Develop realtime web with Scala and XitrumDevelop realtime web with Scala and Xitrum
Develop realtime web with Scala and Xitrum
 
Oak, the architecture of Apache Jackrabbit 3
Oak, the architecture of Apache Jackrabbit 3Oak, the architecture of Apache Jackrabbit 3
Oak, the architecture of Apache Jackrabbit 3
 
Apache Jackrabbit
Apache JackrabbitApache Jackrabbit
Apache Jackrabbit
 
Nodeconf npm 2011
Nodeconf npm 2011Nodeconf npm 2011
Nodeconf npm 2011
 
Unlocking the Magical Powers of WP_Query
Unlocking the Magical Powers of WP_QueryUnlocking the Magical Powers of WP_Query
Unlocking the Magical Powers of WP_Query
 
Best Practices for WordPress in Enterprise
Best Practices for WordPress in EnterpriseBest Practices for WordPress in Enterprise
Best Practices for WordPress in Enterprise
 
Ruby performance - The low hanging fruit
Ruby performance - The low hanging fruitRuby performance - The low hanging fruit
Ruby performance - The low hanging fruit
 
Ansible API
Ansible APIAnsible API
Ansible API
 
JSON REST API for WordPress
JSON REST API for WordPressJSON REST API for WordPress
JSON REST API for WordPress
 
Content Management With Apache Jackrabbit
Content Management With Apache JackrabbitContent Management With Apache Jackrabbit
Content Management With Apache Jackrabbit
 
NoSQL for great good [hanoi.rb talk]
NoSQL for great good [hanoi.rb talk]NoSQL for great good [hanoi.rb talk]
NoSQL for great good [hanoi.rb talk]
 
Actors and Threads
Actors and ThreadsActors and Threads
Actors and Threads
 
Scalable Open Source
Scalable Open SourceScalable Open Source
Scalable Open Source
 
Exciting JavaScript - Part II
Exciting JavaScript - Part IIExciting JavaScript - Part II
Exciting JavaScript - Part II
 
Get Django, Get Hired - An opinionated guide to getting the best job, for the...
Get Django, Get Hired - An opinionated guide to getting the best job, for the...Get Django, Get Hired - An opinionated guide to getting the best job, for the...
Get Django, Get Hired - An opinionated guide to getting the best job, for the...
 
Model with actors and implement with Akka
Model with actors and implement with AkkaModel with actors and implement with Akka
Model with actors and implement with Akka
 
&lt;?php + WordPress
&lt;?php + WordPress&lt;?php + WordPress
&lt;?php + WordPress
 
How to write a web framework
How to write a web frameworkHow to write a web framework
How to write a web framework
 

Similar to Day 2 - Intro to Rails

Ruby on Rails : 簡介與入門
Ruby on Rails : 簡介與入門Ruby on Rails : 簡介與入門
Ruby on Rails : 簡介與入門
Wen-Tien Chang
 
Ruby on-rails-101-presentation-slides-for-a-five-day-introductory-course-1194...
Ruby on-rails-101-presentation-slides-for-a-five-day-introductory-course-1194...Ruby on-rails-101-presentation-slides-for-a-five-day-introductory-course-1194...
Ruby on-rails-101-presentation-slides-for-a-five-day-introductory-course-1194...
Nilesh Panchal
 
Web Development using Ruby on Rails
Web Development using Ruby on RailsWeb Development using Ruby on Rails
Web Development using Ruby on Rails
Avi Kedar
 

Similar to Day 2 - Intro to Rails (20)

Exploring Ruby on Rails and PostgreSQL
Exploring Ruby on Rails and PostgreSQLExploring Ruby on Rails and PostgreSQL
Exploring Ruby on Rails and PostgreSQL
 
Ruby on Rails All Hands Meeting
Ruby on Rails All Hands MeetingRuby on Rails All Hands Meeting
Ruby on Rails All Hands Meeting
 
44CON 2014 - Pentesting NoSQL DB's Using NoSQL Exploitation Framework, Franci...
44CON 2014 - Pentesting NoSQL DB's Using NoSQL Exploitation Framework, Franci...44CON 2014 - Pentesting NoSQL DB's Using NoSQL Exploitation Framework, Franci...
44CON 2014 - Pentesting NoSQL DB's Using NoSQL Exploitation Framework, Franci...
 
2016-05-12 DCRUG React.rb
2016-05-12 DCRUG React.rb2016-05-12 DCRUG React.rb
2016-05-12 DCRUG React.rb
 
Solid and Sustainable Development in Scala
Solid and Sustainable Development in ScalaSolid and Sustainable Development in Scala
Solid and Sustainable Development in Scala
 
Solid And Sustainable Development in Scala
Solid And Sustainable Development in ScalaSolid And Sustainable Development in Scala
Solid And Sustainable Development in Scala
 
Supa fast Ruby + Rails
Supa fast Ruby + RailsSupa fast Ruby + Rails
Supa fast Ruby + Rails
 
Ruby On Rails
Ruby On RailsRuby On Rails
Ruby On Rails
 
web2py:Web development like a boss
web2py:Web development like a bossweb2py:Web development like a boss
web2py:Web development like a boss
 
Ruby on Rails survival guide of an aged Java developer
Ruby on Rails survival guide of an aged Java developerRuby on Rails survival guide of an aged Java developer
Ruby on Rails survival guide of an aged Java developer
 
Ruby on Rails : 簡介與入門
Ruby on Rails : 簡介與入門Ruby on Rails : 簡介與入門
Ruby on Rails : 簡介與入門
 
Wider than rails
Wider than railsWider than rails
Wider than rails
 
Rails Vs CakePHP
Rails Vs CakePHPRails Vs CakePHP
Rails Vs CakePHP
 
About Clack
About ClackAbout Clack
About Clack
 
Ruby on-rails-101-presentation-slides-for-a-five-day-introductory-course-1194...
Ruby on-rails-101-presentation-slides-for-a-five-day-introductory-course-1194...Ruby on-rails-101-presentation-slides-for-a-five-day-introductory-course-1194...
Ruby on-rails-101-presentation-slides-for-a-five-day-introductory-course-1194...
 
Why ruby and rails
Why ruby and railsWhy ruby and rails
Why ruby and rails
 
Web Development using Ruby on Rails
Web Development using Ruby on RailsWeb Development using Ruby on Rails
Web Development using Ruby on Rails
 
Frontend as a first class citizen
Frontend as a first class citizenFrontend as a first class citizen
Frontend as a first class citizen
 
ActiveJDBC - ActiveRecord implementation in Java
ActiveJDBC - ActiveRecord implementation in JavaActiveJDBC - ActiveRecord implementation in Java
ActiveJDBC - ActiveRecord implementation in Java
 
Everything-as-code. A polyglot adventure. #DevoxxPL
Everything-as-code. A polyglot adventure. #DevoxxPLEverything-as-code. A polyglot adventure. #DevoxxPL
Everything-as-code. A polyglot adventure. #DevoxxPL
 

More from Barry Jones (6)

Repeating History...On Purpose...with Elixir
Repeating History...On Purpose...with ElixirRepeating History...On Purpose...with Elixir
Repeating History...On Purpose...with Elixir
 
Day 6 - PostGIS
Day 6 - PostGISDay 6 - PostGIS
Day 6 - PostGIS
 
Protecting Users from Fraud
Protecting Users from FraudProtecting Users from Fraud
Protecting Users from Fraud
 
AWS re:Invent 2013 Recap
AWS re:Invent 2013 RecapAWS re:Invent 2013 Recap
AWS re:Invent 2013 Recap
 
Pair Programming - the lightning talk
Pair Programming - the lightning talkPair Programming - the lightning talk
Pair Programming - the lightning talk
 
What's the "right" PHP Framework?
What's the "right" PHP Framework?What's the "right" PHP Framework?
What's the "right" PHP Framework?
 

Recently uploaded

Indian Escort in Abu DHabi 0508644382 Abu Dhabi Escorts
Indian Escort in Abu DHabi 0508644382 Abu Dhabi EscortsIndian Escort in Abu DHabi 0508644382 Abu Dhabi Escorts
Indian Escort in Abu DHabi 0508644382 Abu Dhabi Escorts
Monica Sydney
 
pdfcoffee.com_business-ethics-q3m7-pdf-free.pdf
pdfcoffee.com_business-ethics-q3m7-pdf-free.pdfpdfcoffee.com_business-ethics-q3m7-pdf-free.pdf
pdfcoffee.com_business-ethics-q3m7-pdf-free.pdf
JOHNBEBONYAP1
 
一比一原版(Flinders毕业证书)弗林德斯大学毕业证原件一模一样
一比一原版(Flinders毕业证书)弗林德斯大学毕业证原件一模一样一比一原版(Flinders毕业证书)弗林德斯大学毕业证原件一模一样
一比一原版(Flinders毕业证书)弗林德斯大学毕业证原件一模一样
ayvbos
 
一比一原版(Offer)康考迪亚大学毕业证学位证靠谱定制
一比一原版(Offer)康考迪亚大学毕业证学位证靠谱定制一比一原版(Offer)康考迪亚大学毕业证学位证靠谱定制
一比一原版(Offer)康考迪亚大学毕业证学位证靠谱定制
pxcywzqs
 
Russian Escort Abu Dhabi 0503464457 Abu DHabi Escorts
Russian Escort Abu Dhabi 0503464457 Abu DHabi EscortsRussian Escort Abu Dhabi 0503464457 Abu DHabi Escorts
Russian Escort Abu Dhabi 0503464457 Abu DHabi Escorts
Monica Sydney
 
哪里办理美国迈阿密大学毕业证(本硕)umiami在读证明存档可查
哪里办理美国迈阿密大学毕业证(本硕)umiami在读证明存档可查哪里办理美国迈阿密大学毕业证(本硕)umiami在读证明存档可查
哪里办理美国迈阿密大学毕业证(本硕)umiami在读证明存档可查
ydyuyu
 
Top profile Call Girls In Dindigul [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Dindigul [ 7014168258 ] Call Me For Genuine Models ...Top profile Call Girls In Dindigul [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Dindigul [ 7014168258 ] Call Me For Genuine Models ...
gajnagarg
 
原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查
原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查
原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查
ydyuyu
 
在线制作约克大学毕业证(yu毕业证)在读证明认证可查
在线制作约克大学毕业证(yu毕业证)在读证明认证可查在线制作约克大学毕业证(yu毕业证)在读证明认证可查
在线制作约克大学毕业证(yu毕业证)在读证明认证可查
ydyuyu
 

Recently uploaded (20)

Vip Firozabad Phone 8250092165 Escorts Service At 6k To 30k Along With Ac Room
Vip Firozabad Phone 8250092165 Escorts Service At 6k To 30k Along With Ac RoomVip Firozabad Phone 8250092165 Escorts Service At 6k To 30k Along With Ac Room
Vip Firozabad Phone 8250092165 Escorts Service At 6k To 30k Along With Ac Room
 
Indian Escort in Abu DHabi 0508644382 Abu Dhabi Escorts
Indian Escort in Abu DHabi 0508644382 Abu Dhabi EscortsIndian Escort in Abu DHabi 0508644382 Abu Dhabi Escorts
Indian Escort in Abu DHabi 0508644382 Abu Dhabi Escorts
 
pdfcoffee.com_business-ethics-q3m7-pdf-free.pdf
pdfcoffee.com_business-ethics-q3m7-pdf-free.pdfpdfcoffee.com_business-ethics-q3m7-pdf-free.pdf
pdfcoffee.com_business-ethics-q3m7-pdf-free.pdf
 
一比一原版(Flinders毕业证书)弗林德斯大学毕业证原件一模一样
一比一原版(Flinders毕业证书)弗林德斯大学毕业证原件一模一样一比一原版(Flinders毕业证书)弗林德斯大学毕业证原件一模一样
一比一原版(Flinders毕业证书)弗林德斯大学毕业证原件一模一样
 
一比一原版(Offer)康考迪亚大学毕业证学位证靠谱定制
一比一原版(Offer)康考迪亚大学毕业证学位证靠谱定制一比一原版(Offer)康考迪亚大学毕业证学位证靠谱定制
一比一原版(Offer)康考迪亚大学毕业证学位证靠谱定制
 
Russian Escort Abu Dhabi 0503464457 Abu DHabi Escorts
Russian Escort Abu Dhabi 0503464457 Abu DHabi EscortsRussian Escort Abu Dhabi 0503464457 Abu DHabi Escorts
Russian Escort Abu Dhabi 0503464457 Abu DHabi Escorts
 
Microsoft Azure Arc Customer Deck Microsoft
Microsoft Azure Arc Customer Deck MicrosoftMicrosoft Azure Arc Customer Deck Microsoft
Microsoft Azure Arc Customer Deck Microsoft
 
哪里办理美国迈阿密大学毕业证(本硕)umiami在读证明存档可查
哪里办理美国迈阿密大学毕业证(本硕)umiami在读证明存档可查哪里办理美国迈阿密大学毕业证(本硕)umiami在读证明存档可查
哪里办理美国迈阿密大学毕业证(本硕)umiami在读证明存档可查
 
Real Men Wear Diapers T Shirts sweatshirt
Real Men Wear Diapers T Shirts sweatshirtReal Men Wear Diapers T Shirts sweatshirt
Real Men Wear Diapers T Shirts sweatshirt
 
Power point inglese - educazione civica di Nuria Iuzzolino
Power point inglese - educazione civica di Nuria IuzzolinoPower point inglese - educazione civica di Nuria Iuzzolino
Power point inglese - educazione civica di Nuria Iuzzolino
 
APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...
APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...
APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...
 
APNIC Updates presented by Paul Wilson at ARIN 53
APNIC Updates presented by Paul Wilson at ARIN 53APNIC Updates presented by Paul Wilson at ARIN 53
APNIC Updates presented by Paul Wilson at ARIN 53
 
20240508 QFM014 Elixir Reading List April 2024.pdf
20240508 QFM014 Elixir Reading List April 2024.pdf20240508 QFM014 Elixir Reading List April 2024.pdf
20240508 QFM014 Elixir Reading List April 2024.pdf
 
"Boost Your Digital Presence: Partner with a Leading SEO Agency"
"Boost Your Digital Presence: Partner with a Leading SEO Agency""Boost Your Digital Presence: Partner with a Leading SEO Agency"
"Boost Your Digital Presence: Partner with a Leading SEO Agency"
 
Top profile Call Girls In Dindigul [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Dindigul [ 7014168258 ] Call Me For Genuine Models ...Top profile Call Girls In Dindigul [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Dindigul [ 7014168258 ] Call Me For Genuine Models ...
 
20240507 QFM013 Machine Intelligence Reading List April 2024.pdf
20240507 QFM013 Machine Intelligence Reading List April 2024.pdf20240507 QFM013 Machine Intelligence Reading List April 2024.pdf
20240507 QFM013 Machine Intelligence Reading List April 2024.pdf
 
best call girls in Hyderabad Finest Escorts Service 📞 9352988975 📞 Available ...
best call girls in Hyderabad Finest Escorts Service 📞 9352988975 📞 Available ...best call girls in Hyderabad Finest Escorts Service 📞 9352988975 📞 Available ...
best call girls in Hyderabad Finest Escorts Service 📞 9352988975 📞 Available ...
 
原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查
原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查
原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查
 
在线制作约克大学毕业证(yu毕业证)在读证明认证可查
在线制作约克大学毕业证(yu毕业证)在读证明认证可查在线制作约克大学毕业证(yu毕业证)在读证明认证可查
在线制作约克大学毕业证(yu毕业证)在读证明认证可查
 
Story Board.pptxrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
Story Board.pptxrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrStory Board.pptxrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
Story Board.pptxrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
 

Day 2 - Intro to Rails

  • 2. What do we look for in a framework? • Please don’t suck – Rails does not suck • Does it follow Model-View-Controller? – Yes – Since Rails 1 it’s been the standard bearer for how to do MVC on the web, copied in almost every language • Does it help me avoid repeating myself (DRY)? – Yes • Is it self documenting? – Yes, it has a set of rules that generally make most documentation unnecessary • Is it flexible enough to bend to my application needs? – Yes • Do other people use it? – Good gosh yes • Will it work with my database? – Yes • Is it still going to be around in X years? – Ruby has Rails – Python has Django – Groovy has Grails – C# has MVC – PHP has fragmented framework Hell (aka – who knows?) – Java has a few major players (Struts 2, Play, etc)
  • 4. Rack Watch this excellent walkthrough of Rack Middleware: http://railscasts.com/episodes/151-rack- middleware Summary: It’s a layer of ruby code that passes requests into your app and sends responses back out. You can add layers to do pre/post processing on all requests prior to beginning ANY of your application code.
  • 5. Before the Request • Configuration • Initializers • Gems • Environments • Asset Pipeline
  • 6. Models / ActiveRecord class Post < ActiveRecord::Base belongs_to :category has_many :tags, through: :posts_tags validates :title, presence: true before_save :create_slug, only: :create scope :newest_first, order(‘created_at DESC’) scope :active, where(‘active = ?’,true) scope :newest_active, newest_first.active scope :search, lambda do |text| where(‘title LIKE ?’,”%#{text}%”) end def create_slug self.slug = title.downcase.squish.sub(‘ ‘,’-’) end end post = Post.new(title: ‘Some title’) post.save! OR post = Post.create(title: ‘Some title’) post.slug # some-title post.id # 1 post.created_at # Created datetime post.updated_at # Updated datetime post.title = ‘New title’ post.save! # Relations post.tags.first post.tags.count post.category.name post = Post.include(:tags) # Eager load post = Post.search(‘some’).newest_active.first
  • 7. Migrations class CreateInitialTables < ActiveRecord::Migration def up create_table :posts do |t| t.string :title t.text :body t.string :slug t.integer :category_id t.timestamps end # … create more tables… add_index :tags, [:name,:something], unique: true execute “UPDATE posts SET field = ‘value’ WHERE stuff = ‘happens’” end def down drop_table :posts end def change add_column :posts, :user_id, :integer end end $ rake db:migrate
  • 8. Controllers and REST Class PostsController < ApplicationController before_filter :authenticate, only: :destroy def index # GET /posts end def new # GET /posts/new end def create # POST /posts end def show # GET /posts/:id end def edit # GET /posts/:id/edit end def update # PUT /posts/:id end def destroy # DELETE /posts/:id end end # Routes resources :posts OR limit it resources :posts, only: [:create,:new]
  • 9. Views /app/views /layouts /application.html.erb /posts /new.html.slim /new.json.rabl /index.xml.erb /_widget.html.erb # slim example .post h2=post.title .body.grid-8=post.body # erb example <div class=“post”> <h2><%=post.title%></h2> <div class=“body grid-8”> <%=post.body%> </div> </div>
  • 10. Generators • rails new . • rails g scaffold article name content:text published_on:date

Editor's Notes

  1. This is a bad diagram because I tried to use the built in tools in power point. I need to update it. In reality the View arrow should be going back through Rack to the Browser.