Rails Kool-Aid™
Giving you your very own
glass, drink up!
Saturday, November 20, 2010
Me llamo Ryan Abbott
Co-founder of Loudpixel Inc.
Developing web sites/apps for nearly 10
years #angelfirecounts
Largest production Rails application is
Levee #itsawesome
Ten-ish other production apps
HI, WHO ARE YOU AGAIN?
Saturday, November 20, 2010
WHO/WHAT IS RUBY ON RAILS?
Web Framework built on top of Ruby
Created by David Heinemeier Hansson
DHH released Rails as open source in July
of 2004 but was the sole contributor
Rails 1.0 was released on December 13,
2005
Rails 3.0 (current) was released on
August 29, 2010 - it has 1700+
contributors
Saturday, November 20, 2010
WHY RAILS?
Rapid Application Development
Defaults - Scaffold, relationships,
nested objects, etc.
Plugins!
Deployment
Sites like Heroku provide free hosting,
and all you need is GIT
Community
1700+ contributors and tens of thousands
of live apps
Saturday, November 20, 2010
LITTLE BIG WORDS
CRUD (Create, Read, Update, Delete)
The 4 basic functions performed on the
data that our application stores
MVC (Model-View-Controller)
The architecture pattern used by rails
DRY (Don’t Repeat Yourself)
Software development principle
emphasized within the rails community
REST (Representative State Transfer)
Using identifiers to represent resources
Saturday, November 20, 2010
MODEL-VIEW-CONTROLLER
Models
Manages how the data is stored, and how
that data behaves
Views
Renders the models in a way best suited
for the requested format
Controllers
Receives instructions from users based
on actions, and informs the models how
they should respond and the views what
they should display
Saturday, November 20, 2010
REST[ful] RESOURCES
RESTful URLs provide a human readable
set of resources, and their states.
An example could be
/users/15/edit
What do we expect here?
User
Id of 15
Edit view to be displayed
Saturday, November 20, 2010
CREATING A RAILS PROJECT
> rails new blogpress
Creates a new directory called ‘blogpress’
containing the following:
Gemfile
Home to your GEM dependencies
app
This is where you’ll find your models,
views, and controllers
config
As you would imagine, here lives your
configurations; routes, etc.
Saturday, November 20, 2010
DIRECTORY BREAKDOWN CON’T
db
Contains your database schema and
migrations
log
Development, Test, and Production logs
public
Web accessible directory, images,
stylesheets, javascript, etc.
vendor
A centralized location for third-party
code from plugins to gem source
Saturday, November 20, 2010
APPLICATION READY, NEXT?
> bundle install
Bundler is a tool that installs required
gems for your application. Bundler is
able to determine dependencies and
children without instructions from you
database configuration (database.yml)
Defaults to sqlite, allows for MySQL,
and PostgreSQL OOB, and others with
use of plugins
development, test, production
Saturday, November 20, 2010
DATABASE CONFIGURED, SO...
> rake db:create
This will read the database.yml file
and create the databases requested
rake, WTF?
Rake is a way to run tasks, the
majority of tasks you run are already
provided by Rails, but you can always
create your own
> rake -T
Display a list of all rake tasks
available
Saturday, November 20, 2010
HELLO, WORLD? RAILS?
> rails server
Starts the rails server at port 3000 and
allows us to navigate our new site!
Check things out, in the browser view:
http://127.0.0.1:3000/
Saturday, November 20, 2010
Saturday, November 20, 2010
Saturday, November 20, 2010
DATABASE CONFIGURED, SO...
> rm public/index.html
We don’t want this page displaying
when visitors come to our blog
> rails generate controller home index
Generate a controller with only an
index action to take the place of
public/index.html
Update config/routes.rb to let the app
know that our root should be
REMOVE get "home/index"
ADD root :to => “home#index”
Saturday, November 20, 2010
GOODBYE DEFAULT
Saturday, November 20, 2010
WHAT MAKES UP A BLOG?
Saturday, November 20, 2010
EVERY BLOG HAS POSTS
> rails generate scaffold Post
author:string title:string content:text
permalink:text
Scaffold creates full CRUD
functionality for us, including our
model, view, controller, and even our
database migration
rake db:migrate
Calls our self.up method to create the
posts database table
Saturday, November 20, 2010
OH POSTS, WHERE ART THOU?
In the browser, lets have a look at:
http://127.0.0.1:3000/posts
Saturday, November 20, 2010
WHAT’S MISSING?
Posts
Comments (haters gone hate)
Tags / Keywords (I’m here for ONE thing)
Validation (plan for the spoon in the knife drawer)
Overall Blog
Authentication (hackers gone hack)
Most Recent Posts (laziness is contagious)
Saturday, November 20, 2010
COMMENTS
> rails generate scaffold Comment
post:references email:string
content:text
> rake db:migrate
config/routes.rb
1307656, 1307661
Saturday, November 20, 2010
ROUTING FOR COMMENTS
/comments
/comments/1
/comments/1/edit
/posts
/posts/1
/posts/1/edit
/posts/1/comments
/posts/1/comments/1
/posts/1/comments/edit
/comments?post_id=1
/comments/1?post_id=1
/comments/1/edit?post_id=1
@post = Post.
find(params[:post_id])
@comments = @post.comments
1307656, 1307661
Saturday, November 20, 2010
COMMENTS
<h2>Comments</h2>
<% @post.comments.each do |comment| %>
<p>
<b><%= comment.email %></b>
<%= comment.content %>
</p>
<% end %>
<h2>Add a comment:</h2>
<%= form_for([@post, @post.comments.build]) do |f| %>
<% if @comment && @comment.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@comment.errors.count, "error") %> prohibited this comment from being saved:</h2>
<ul>
<% @comment.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :email %><br />
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :content %><br />
<%= f.text_area :content %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
app/views/posts/show.html.erb
@post = Post.find(params[:post_id])
@comment = @post.comments.new(params[:comment])
respond_to do |format|
if @comment.save
format.html { redirect_to(post_path(@post), :notice => 'Comment was successfully
created.') }
format.xml { render :xml => @post, :status => :created, :location => post_path(@post) }
else
format.html { redirect_to post_path(@post) }
format.xml { render :xml => @comment.errors, :status => :unprocessable_entity }
end
end
app/views/controllers/comments_controller.rb
has_many :comments
app/models/post.rb
Saturday, November 20, 2010
SHOWING COMMENTS
1307643
Saturday, November 20, 2010
ACCEPTING COMMENTS
1314025
Saturday, November 20, 2010
BRING ON THE SPAM!
Saturday, November 20, 2010
TAGS / KEYWORDS
gem 'acts-as-taggable-on'
> bundle install
> rails generate acts_as_taggable_on:migration
> rake db:migrate
acts_as_taggable
1307777
Saturday, November 20, 2010
VALIDATION OPTIONS
:presence => true
:uniqueness => true
:numericality => true
:length => { :minimum => 0, maximum => 2000 }
:format => { :with => /.*/ }
:inclusion => { :in => [1,2,3] }
:exclusion => { :in => [1,2,3] }
:acceptance => true
:confirmation => true
Saturday, November 20, 2010
VALIDATION
Comment Validation
Post Validation
1307783, 1307789
Saturday, November 20, 2010
AUTHENTICATION
Basic HTTP authentication prompts users
via a browser popup. This authentication
lasts until the browser is closed.
1307757
Saturday, November 20, 2010
MOST RECENT POSTS
1309253
We want to display the top n posts on the main page,
of course we’ll want the n more recent posts
Saturday, November 20, 2010
JOBS, GIGS, CAREERS
http://jobs.37signals.com/
http://toprubyjobs.com/
http://jobs.rubynow.com/
http://www.railsjob.com/
http://railswork.com/
http://weblog.rubyonrails.org/jobs
http://www.rorjobs.com/
http://www.railslodge.com/jobs
http://ruby.jobmotel.com/
Saturday, November 20, 2010
HOSTING, DEPLOYMENT
http://heroku.com/
http://engineyard.com/
http://www.webbynode.com/
http://www.rubyenterpriseedition.com/
https://github.com/
Saturday, November 20, 2010
ONLINE RESOURCES?
http://tryruby.org/
A 15 minute, in-browser, tutorial showing you the
basics of Ruby
http://railsforzombies.org/
Online interactive tutorials that teach you rails
right in the browser
http://rubyonrails.org/screencasts/rails3
A set of beautiful screencasts by Gregg Pollack that
walk viewers though the details of the Rails core
pieces
http://railscasts.com/
Extremely helpful videos from Ryan Bates - started
back in 2007 Ryan is always on top of new plugins
and methods of doing things
Saturday, November 20, 2010
Rails Kool-Aid™
Ryan Abbott
@MSURabbott
ryan@loudpixel.com
Saturday, November 20, 2010
Rails Kool-Aid™
Ryan Abbott
@MSURabbott
ryan@loudpixel.com
http://spkr8.com/t/5139
http://slidesha.re/b09DCa
Saturday, November 20, 2010

Ruby on-rails-workshop

  • 1.
    Rails Kool-Aid™ Giving youyour very own glass, drink up! Saturday, November 20, 2010
  • 2.
    Me llamo RyanAbbott Co-founder of Loudpixel Inc. Developing web sites/apps for nearly 10 years #angelfirecounts Largest production Rails application is Levee #itsawesome Ten-ish other production apps HI, WHO ARE YOU AGAIN? Saturday, November 20, 2010
  • 3.
    WHO/WHAT IS RUBYON RAILS? Web Framework built on top of Ruby Created by David Heinemeier Hansson DHH released Rails as open source in July of 2004 but was the sole contributor Rails 1.0 was released on December 13, 2005 Rails 3.0 (current) was released on August 29, 2010 - it has 1700+ contributors Saturday, November 20, 2010
  • 4.
    WHY RAILS? Rapid ApplicationDevelopment Defaults - Scaffold, relationships, nested objects, etc. Plugins! Deployment Sites like Heroku provide free hosting, and all you need is GIT Community 1700+ contributors and tens of thousands of live apps Saturday, November 20, 2010
  • 5.
    LITTLE BIG WORDS CRUD(Create, Read, Update, Delete) The 4 basic functions performed on the data that our application stores MVC (Model-View-Controller) The architecture pattern used by rails DRY (Don’t Repeat Yourself) Software development principle emphasized within the rails community REST (Representative State Transfer) Using identifiers to represent resources Saturday, November 20, 2010
  • 6.
    MODEL-VIEW-CONTROLLER Models Manages how thedata is stored, and how that data behaves Views Renders the models in a way best suited for the requested format Controllers Receives instructions from users based on actions, and informs the models how they should respond and the views what they should display Saturday, November 20, 2010
  • 7.
    REST[ful] RESOURCES RESTful URLsprovide a human readable set of resources, and their states. An example could be /users/15/edit What do we expect here? User Id of 15 Edit view to be displayed Saturday, November 20, 2010
  • 8.
    CREATING A RAILSPROJECT > rails new blogpress Creates a new directory called ‘blogpress’ containing the following: Gemfile Home to your GEM dependencies app This is where you’ll find your models, views, and controllers config As you would imagine, here lives your configurations; routes, etc. Saturday, November 20, 2010
  • 9.
    DIRECTORY BREAKDOWN CON’T db Containsyour database schema and migrations log Development, Test, and Production logs public Web accessible directory, images, stylesheets, javascript, etc. vendor A centralized location for third-party code from plugins to gem source Saturday, November 20, 2010
  • 10.
    APPLICATION READY, NEXT? >bundle install Bundler is a tool that installs required gems for your application. Bundler is able to determine dependencies and children without instructions from you database configuration (database.yml) Defaults to sqlite, allows for MySQL, and PostgreSQL OOB, and others with use of plugins development, test, production Saturday, November 20, 2010
  • 11.
    DATABASE CONFIGURED, SO... >rake db:create This will read the database.yml file and create the databases requested rake, WTF? Rake is a way to run tasks, the majority of tasks you run are already provided by Rails, but you can always create your own > rake -T Display a list of all rake tasks available Saturday, November 20, 2010
  • 12.
    HELLO, WORLD? RAILS? >rails server Starts the rails server at port 3000 and allows us to navigate our new site! Check things out, in the browser view: http://127.0.0.1:3000/ Saturday, November 20, 2010
  • 13.
  • 14.
  • 15.
    DATABASE CONFIGURED, SO... >rm public/index.html We don’t want this page displaying when visitors come to our blog > rails generate controller home index Generate a controller with only an index action to take the place of public/index.html Update config/routes.rb to let the app know that our root should be REMOVE get "home/index" ADD root :to => “home#index” Saturday, November 20, 2010
  • 16.
  • 17.
    WHAT MAKES UPA BLOG? Saturday, November 20, 2010
  • 18.
    EVERY BLOG HASPOSTS > rails generate scaffold Post author:string title:string content:text permalink:text Scaffold creates full CRUD functionality for us, including our model, view, controller, and even our database migration rake db:migrate Calls our self.up method to create the posts database table Saturday, November 20, 2010
  • 19.
    OH POSTS, WHEREART THOU? In the browser, lets have a look at: http://127.0.0.1:3000/posts Saturday, November 20, 2010
  • 20.
    WHAT’S MISSING? Posts Comments (hatersgone hate) Tags / Keywords (I’m here for ONE thing) Validation (plan for the spoon in the knife drawer) Overall Blog Authentication (hackers gone hack) Most Recent Posts (laziness is contagious) Saturday, November 20, 2010
  • 21.
    COMMENTS > rails generatescaffold Comment post:references email:string content:text > rake db:migrate config/routes.rb 1307656, 1307661 Saturday, November 20, 2010
  • 22.
  • 23.
    COMMENTS <h2>Comments</h2> <% @post.comments.each do|comment| %> <p> <b><%= comment.email %></b> <%= comment.content %> </p> <% end %> <h2>Add a comment:</h2> <%= form_for([@post, @post.comments.build]) do |f| %> <% if @comment && @comment.errors.any? %> <div id="error_explanation"> <h2><%= pluralize(@comment.errors.count, "error") %> prohibited this comment from being saved:</h2> <ul> <% @comment.errors.full_messages.each do |msg| %> <li><%= msg %></li> <% end %> </ul> </div> <% end %> <div class="field"> <%= f.label :email %><br /> <%= f.text_field :email %> </div> <div class="field"> <%= f.label :content %><br /> <%= f.text_area :content %> </div> <div class="actions"> <%= f.submit %> </div> <% end %> app/views/posts/show.html.erb @post = Post.find(params[:post_id]) @comment = @post.comments.new(params[:comment]) respond_to do |format| if @comment.save format.html { redirect_to(post_path(@post), :notice => 'Comment was successfully created.') } format.xml { render :xml => @post, :status => :created, :location => post_path(@post) } else format.html { redirect_to post_path(@post) } format.xml { render :xml => @comment.errors, :status => :unprocessable_entity } end end app/views/controllers/comments_controller.rb has_many :comments app/models/post.rb Saturday, November 20, 2010
  • 24.
  • 25.
  • 26.
    BRING ON THESPAM! Saturday, November 20, 2010
  • 27.
    TAGS / KEYWORDS gem'acts-as-taggable-on' > bundle install > rails generate acts_as_taggable_on:migration > rake db:migrate acts_as_taggable 1307777 Saturday, November 20, 2010
  • 28.
    VALIDATION OPTIONS :presence =>true :uniqueness => true :numericality => true :length => { :minimum => 0, maximum => 2000 } :format => { :with => /.*/ } :inclusion => { :in => [1,2,3] } :exclusion => { :in => [1,2,3] } :acceptance => true :confirmation => true Saturday, November 20, 2010
  • 29.
    VALIDATION Comment Validation Post Validation 1307783,1307789 Saturday, November 20, 2010
  • 30.
    AUTHENTICATION Basic HTTP authenticationprompts users via a browser popup. This authentication lasts until the browser is closed. 1307757 Saturday, November 20, 2010
  • 31.
    MOST RECENT POSTS 1309253 Wewant to display the top n posts on the main page, of course we’ll want the n more recent posts Saturday, November 20, 2010
  • 32.
  • 33.
  • 34.
    ONLINE RESOURCES? http://tryruby.org/ A 15minute, in-browser, tutorial showing you the basics of Ruby http://railsforzombies.org/ Online interactive tutorials that teach you rails right in the browser http://rubyonrails.org/screencasts/rails3 A set of beautiful screencasts by Gregg Pollack that walk viewers though the details of the Rails core pieces http://railscasts.com/ Extremely helpful videos from Ryan Bates - started back in 2007 Ryan is always on top of new plugins and methods of doing things Saturday, November 20, 2010
  • 35.
  • 36.

Editor's Notes

  • #4 - Released Ruby on Rails as open source in July 2004, but did not share commit rights to the project until February 2005.
  • #5 - Released Ruby on Rails as open source in July 2004, but did not share commit rights to the project until February 2005.
  • #6 - Released Ruby on Rails as open source in July 2004, but did not share commit rights to the project until February 2005.
  • #7 - Released Ruby on Rails as open source in July 2004, but did not share commit rights to the project until February 2005.
  • #8 - Released Ruby on Rails as open source in July 2004, but did not share commit rights to the project until February 2005.