Your SlideShare is downloading. ×
Rails 2010 Workshop
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Rails 2010 Workshop

776
views

Published on

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

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
776
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
6
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
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 => “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
  • Transcript

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

    ×