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...
map.connect ‘/guess/daniel’, :controller => ‘guess’, :action => ‘right’ map.connect ‘/guess/:name’, :controller => ‘guess’...
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...
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> <...
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  | +--------+--------+-...
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, ema...
>> 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::RecordInval...
Scopes
class ItpStudent < ActiveRecord::Base named_scope :passing => { :conditions => “gpa > 2” } named_scope :advised_by => {|x|...
>> 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… <ul><li>Partials </li></ul><ul><li>Helpers (built-in, user-defined) </li></ul><ul><li>Testing </li></...
Further Reading <ul><li>ruby-doc.org </li></ul><ul><li>www.rubyonrails.org </li></ul><ul><li>guides.rubyonrails.org </li><...
Thanks!
Upcoming SlideShare
Loading in...5
×

Rails 2010 Workshop

804

Published on

I gave this talk at ITP @ NYU April 7, 2010.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
804
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
6
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • 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.
  • http://itp.nyu.edu/~dmt321/bad-php.txt http://itp.nyu.edu/~dmt321/better-php.txt
  • 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.
  • Comments
  • table names, primary key, location of files
  • EXAMPLES
  • You can created Named Routes as well – this creates the hello_url and hello_path helpers.
  • Adding parameter :name
  • More sane way to do it  Also notice that :action =&gt; “index” is optional
  • Convention over configuration – Rails knows where to look for views.
  • Convention over configuration – Rails knows where to look for layouts.
  • Removed render :text code
  • ORM is Object-Relational Mapping. It converts a row in a database table to an object.
  • This is a lie. In fact Rails would not build a query like this.
  • There are three different environments (more are possible). We will work primarily with the “development” environment.
  • Notice the fields that were not in the migration file id - more convention over configuration
  • Chained scopes
  • Convention over configuration – looks for itp_student.advisor_id and classes.itp_student_id
  • Rails 2010 Workshop

    1. 1. Ruby on Rails Workshop NYU ITP 2010 Presented by Daniel Tsadok
    2. 2. What is Ruby on Rails?
    3. 3. Why Ruby on Rails? Why not PHP?
    4. 4. Organization
    5. 5. Rails 2 vs. Rails 3
    6. 6. Ruby
    7. 7. Run irb in the terminal OR Create a text file (.rb) and run: ruby filename.rb
    8. 8. puts(“Hello, World!”)
    9. 9. #puts(“Hello, World!”)
    10. 10. s = “Hello, World!” puts(s)
    11. 11. def say_hello(name) puts(“Hello, #{name}!”) end say_hello(“Daniel”)
    12. 12. #look Ma, no parentheses! def say_hello name puts “Hello, #{name}!” end say_hello “Daniel”
    13. 13. Everything is an Object
    14. 14. 1 + 1
    15. 15. 1.class 1.methods 1.+(1)
    16. 16. class Fixnum alias :+ :plus end one = 1 one.plus(1)
    17. 17. Loop It Up
    18. 18. ar = [1, 2, 3] ar.length for i in ar puts i end
    19. 19. 3.times do puts “Hip, Hip, Hooray!” end
    20. 20. ar = [1, 2, 3] ar.length ar.each do |i| puts i end
    21. 21. ar = %w(Red Green Blue) puts ar.inspect ar.each {|w| puts w} puts ar[1]
    22. 22. h = { “ r” => “Red”, “ g” => “Green”, “ b” => “Blue” } h.keys h.each do |k, v| puts “#{k}: #{v}” end
    23. 23. Rails
    24. 24. Rails Principles
    25. 25. Convention Over Configuration
    26. 26. Don’t Repeat Yourself
    27. 27. Model View Controller
    28. 28. rails hello_rails
    29. 29. Controller
    30. 30. script/generate script/generate controller script/generate controller hello
    31. 31. app/controllers/hello_controller.rb def index render :text => “Hello!” end
    32. 32. Routes
    33. 33. config/routes.rb map.connect ‘:controller/:action/:id`
    34. 34. map.connect ‘/hello’, :controller => ‘hello’, :action => ‘index’
    35. 35. map. hello ‘/hello’, :controller => ‘hello’, :action => ‘index’
    36. 36. script/server http://localhost:3000/hello
    37. 37. map.connect ‘/hello/ :name ’, :controller => ‘hello’, :action => ‘index’
    38. 38. def index render :text => “Hello, #{params[:name]}!” end
    39. 39. script/generate controller guess
    40. 40. map.connect ‘/guess/:name’, :controller => ‘guess’ --------------------------------- def index if params[:name] == “daniel” render :text => “You got it!” else render :text => “Not quite!” end end
    41. 41. map.connect ‘/guess/daniel’, :controller => ‘guess’, :action => ‘right’ map.connect ‘/guess/:name’, :controller => ‘guess’, :action => ‘wrong’
    42. 42. app/controllers/guess_controller.rb def right render :text => “You got it!” end def wrong render :text => “Not quite!” end
    43. 43. Filters
    44. 44. 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
    45. 45. View
    46. 46. app/views/guess/right.html.erb --- <h1>Right!</h1> app/views/guess/wrong.html.erb --- <h1>It’s not <%= params[:name] %></h1>
    47. 47. Layouts
    48. 48. app/views/layouts/main.html.erb
    49. 49. <html> <head> <title>Title Goes Here</title> </head> <body> <div id=“header”>…</div> <div id=“main”> <%= yield %> </div> <div id=“footer”>…</div> </body> </html>
    50. 50. app/controllers/guess_controller.rb class GuestController < Applica... layout “main” def right end def wrong end end
    51. 51. Model
    52. 52. ORM
    53. 53. mysql> select * from itp_students; +--------+--------+----------------+ | name | email | advisor | +--------+--------+----------------+ | Daniel | dmt321 | Shawn | | Bob | brb555 | DanO | +--------+--------+----------------+
    54. 54. student = ItpStudent.new student.name = “Daniel” student.email = “...” student.advisor = “Shawn” student.save
    55. 55. INSERT INTO itp_students (name, email, advisor) VALUES (‘Daniel’, ‘...’, ‘Shawn’);
    56. 56. student = ItpStudent.find_by_name “Daniel” student.name = “Daniel T” student.save
    57. 57. UPDATE itp_students SET name = ‘Daniel T’ WHERE name = ‘Daniel’;
    58. 58. config/database.yml
    59. 59. script/generate model ItpStudent name:string email:string advisor:string
    60. 60. db/migrate/..._create_itp_students.rb
    61. 61. script/generate migration add_gpa_to_itp_student gpa:integer
    62. 62. rake db:migrate
    63. 63. $ 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)
    64. 64. >> student = ItpStudent.new >> student.name = “Daniel” >> student.save!
    65. 65. Validations
    66. 66. class ItpStudent < ActiveRecord::Base validates_presence_of :name end
    67. 67. >> student = ItpStudent.new >> student.valid? => false >> student.save => false >> student.save! ActiveRecord::RecordInvalid: Validation failed: Name can't be blank
    68. 68. Scopes
    69. 69. class ItpStudent < ActiveRecord::Base named_scope :passing => { :conditions => “gpa > 2” } named_scope :advised_by => {|x| :conditions => {:advisor => x} } end
    70. 70. >> ItpStudent.passing >> ItpStudent.advised_by ‘Shawn’ >> ItpStudent.advised_by(‘Shawn’).passing
    71. 71. Relations (has_many, belongs_to)
    72. 72. class ItpStudent < ActiveRecord::Base has_many :classes belongs_to :itp_class end
    73. 73. >> student = ItpStudent.first >> student.classes.first
    74. 74. MOAR !!!
    75. 75. If you got this far… <ul><li>Partials </li></ul><ul><li>Helpers (built-in, user-defined) </li></ul><ul><li>Testing </li></ul><ul><li>External libraries (plugins, gems) </li></ul>
    76. 76. Further Reading <ul><li>ruby-doc.org </li></ul><ul><li>www.rubyonrails.org </li></ul><ul><li>guides.rubyonrails.org </li></ul><ul><li>api.rubyonrails.org </li></ul><ul><li>planetrubyonrails.com </li></ul><ul><li>railscasts.com </li></ul><ul><li>peepcode.com </li></ul>
    77. 77. Thanks!

    ×