Ruby on Rails Workshop NYU ITP 2010 Presented by Daniel Tsadok
What is Ruby on Rails?
Why Ruby on Rails? Why not PHP?
Organization
Rails 2 vs. Rails 3
Ruby
Run  irb  in the terminal OR Create a text file (.rb) and run:  ruby filename.rb
puts(“Hello, World!”)
#puts(“Hello, World!”)
s = “Hello, World!” puts(s)
def say_hello(name) puts(“Hello, #{name}!”) end say_hello(“Daniel”)
#look Ma, no parentheses! def say_hello name puts “Hello, #{name}!” end say_hello “Daniel”
Everything is an Object
1 + 1
1.class 1.methods 1.+(1)
class Fixnum alias :+ :plus end one = 1 one.plus(1)
Loop It Up
ar = [1, 2, 3] ar.length for i in ar puts i end
3.times do puts “Hip, Hip, Hooray!” end
ar = [1, 2, 3] ar.length ar.each do |i| puts i end
ar = %w(Red Green Blue) puts ar.inspect ar.each {|w| puts w} puts ar[1]
h = { “ r” => “Red”, “ g” => “Green”, “ b” => “Blue” } h.keys h.each do |k, v| puts “#{k}: #{v}” end
Rails
Rails Principles
Convention Over Configuration
Don’t Repeat Yourself
Model View Controller
rails hello_rails
Controller
script/generate script/generate controller script/generate controller hello
app/controllers/hello_controller.rb def index render :text => “Hello!” end
Routes
config/routes.rb map.connect ‘:controller/:action/:id`
map.connect ‘/hello’, :controller => ‘hello’, :action => ‘index’
map. hello  ‘/hello’, :controller => ‘hello’, :action => ‘index’
script/server http://localhost:3000/hello
map.connect ‘/hello/ :name ’, :controller => ‘hello’, :action => ‘index’
def index render :text => “Hello, #{params[:name]}!” end
script/generate controller guess
map.connect ‘/guess/:name’, :controller => ‘guess’ --------------------------------- def index if params[:name] == “daniel” render :text => “You got it!” else render :text => “Not quite!” end end
map.connect ‘/guess/daniel’, :controller => ‘guess’, :action => ‘right’ map.connect ‘/guess/:name’, :controller => ‘guess’, :action => ‘wrong’
app/controllers/guess_controller.rb def right render :text => “You got it!” end def wrong render :text => “Not quite!” end
Filters
app/controllers/hello_controller.rb before_filter :check_id def index end protected def check_id if params[:id] != “Daniel” redirect_to “http://www.nyu.edu” end end
View
app/views/guess/right.html.erb --- <h1>Right!</h1> app/views/guess/wrong.html.erb --- <h1>It’s not <%= params[:name] %></h1>
Layouts
app/views/layouts/main.html.erb
<html> <head> <title>Title Goes Here</title> </head> <body> <div id=“header”>…</div> <div id=“main”> <%= yield %> </div> <div id=“footer”>…</div> </body> </html>
app/controllers/guess_controller.rb class GuestController < Applica... layout “main” def right end def wrong end end
Model
ORM
mysql> select * from itp_students; +--------+--------+----------------+ | name  | email  | advisor  | +--------+--------+----------------+ | Daniel | dmt321 | Shawn  | | Bob  | brb555 | DanO  | +--------+--------+----------------+
student = ItpStudent.new student.name = “Daniel” student.email = “...” student.advisor = “Shawn” student.save
INSERT INTO itp_students (name, email, advisor) VALUES (‘Daniel’, ‘...’, ‘Shawn’);
student = ItpStudent.find_by_name “Daniel” student.name = “Daniel T” student.save
UPDATE itp_students SET name = ‘Daniel T’ WHERE name = ‘Daniel’;
config/database.yml
script/generate model ItpStudent name:string email:string advisor:string
db/migrate/..._create_itp_students.rb
script/generate migration add_gpa_to_itp_student gpa:integer
rake db:migrate
$ script/console Loading development environment (Rails 2.3.5) >>  ItpStudent => ItpStudent(id: integer, name: string, email: string, advisor: string, created _at: datetime, updated_at: datetime, gpa: integer)
>> student = ItpStudent.new >> student.name = “Daniel” >> student.save!
Validations
class ItpStudent < ActiveRecord::Base validates_presence_of :name end
>> student = ItpStudent.new >> student.valid? => false >> student.save => false >> student.save! ActiveRecord::RecordInvalid: Validation failed: Name can't be blank
Scopes
class ItpStudent < ActiveRecord::Base named_scope :passing => { :conditions => “gpa > 2” } named_scope :advised_by => {|x| :conditions => {:advisor => x} } end
>> ItpStudent.passing >> ItpStudent.advised_by ‘Shawn’ >> ItpStudent.advised_by(‘Shawn’).passing
Relations (has_many, belongs_to)
class ItpStudent < ActiveRecord::Base has_many :classes belongs_to :itp_class end
>> student = ItpStudent.first >> student.classes.first
MOAR !!!
If you got this far… Partials Helpers (built-in, user-defined) Testing External libraries (plugins, gems)
Further Reading ruby-doc.org www.rubyonrails.org guides.rubyonrails.org api.rubyonrails.org planetrubyonrails.com railscasts.com peepcode.com
Thanks!

Rails 2010 Workshop

Editor's Notes

  • #3 Ruby on Rails is a web development framework that makes building complex web applications much easier. Ruby is the language in which Rails is built.
  • #4 http://itp.nyu.edu/~dmt321/bad-php.txt http://itp.nyu.edu/~dmt321/better-php.txt
  • #6 Rails 3 is on the verge of being released – it’s already out in beta. Some of the syntax, especially in routing, will be changed in Rails 3. But the fundamental concepts are the same.
  • #10 Comments
  • #26 table names, primary key, location of files
  • #27 EXAMPLES
  • #36 You can created Named Routes as well – this creates the hello_url and hello_path helpers.
  • #38 Adding parameter :name
  • #41 More sane way to do it  Also notice that :action =&gt; “index” is optional
  • #47 Convention over configuration – Rails knows where to look for views.
  • #49 Convention over configuration – Rails knows where to look for layouts.
  • #51 Removed render :text code
  • #53 ORM is Object-Relational Mapping. It converts a row in a database table to an object.
  • #58 This is a lie. In fact Rails would not build a query like this.
  • #59 There are three different environments (more are possible). We will work primarily with the “development” environment.
  • #64 Notice the fields that were not in the migration file id - more convention over configuration
  • #71 Chained scopes
  • #73 Convention over configuration – looks for itp_student.advisor_id and classes.itp_student_id