• Like
An Introduction to Ruby on Rails
Upcoming SlideShare
Loading in...5
×

An Introduction to Ruby on Rails

  • 2,599 views
Uploaded on

Which ORM (Object-relational mapping) library should I use? What should I use for my template language? Should I use MVC (Model/View/Controller) or some other pattern? What unit testing framework …

Which ORM (Object-relational mapping) library should I use? What should I use for my template language? Should I use MVC (Model/View/Controller) or some other pattern? What unit testing framework should I use? What database engine should I use?

Unfortunately, these questions make it difficult to start web projects. Therefore, Ruby on Rails has made all of these decisions already; you can just get started. And it's extensible enough that if one of the default choices doesn't work for you, just choose a different library. How does Ruby on Rails answer these questions? How does it use the Ruby language to help make your development even faster? Come and find out!

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
2,599
On Slideshare
0
From Embeds
0
Number of Embeds
2

Actions

Shares
Downloads
56
Comments
0
Likes
3

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
  • TAKE IT SLOW
    Pause after topics
    Take some deep breaths
    Walk to other side of projector when pausing


  • Within3
  • - Ecommerce Sites on Shopify
    - iPad/iPhone R&D
    - Consulting
  • - Ecommerce Sites on Shopify
    - iPad/iPhone R&D
    - Consulting



  • If I throw out a term that you don’t understand, please feel free to ask!
  • If I throw out a term that you don’t understand, please feel free to ask!
  • If I throw out a term that you don’t understand, please feel free to ask!
  • If I throw out a term that you don’t understand, please feel free to ask!



  • Write once, use everywhere
    Code is easier to consume
    Easier to edit later


  • How many have not used MVC?

    It’s up to you to architect and organize your code in those tools. Rails makes that decision for you by implementing MVC.

  • Code driving the UI that gets displayed to the user
  • Interacts with model and sends the data to the view
  • - Open source framework for developing web applications
    - Created by David Heinemeier Hansson
    - 1.0 was an extremely opinionated framework
    - 3.0 allows you to change many of the opinions
    …Written in the language
  • - Everything has a type, but the language infers types at runtime instead of you specifying them up front



  • Remembering names is not important; knowing how the pieces fit together & interact is



  • Utility collection and standard Ruby core extensions used both by Rails itself and your application
  • The glue; ties together framework libraries and 3rd party plugins
  • Build an application through the life of an HTTP request
  • - List 10 most recent posts, newest first





  • ...since for most of you this is your first time seeing Ruby code, let’s look at this a little bit


  • any instance variables in a controller are automatically handed into the view
  • ...since for most of you this is your first time seeing Ruby code, let’s look at this a little bit



  • ActiveRecord is....
    Default option, but you can use any ORM that implements ActiveModel



  • Inheriting from ActiveRecord::Base does some pretty cool things for us
  • Because I have a class called Post AR knows to look up...

    it also gives us some more time saving methods like...



  • find_by_title will call SQL right away
    .where (and other query methods) will delegate the SQL call to the first time the array is used
  • Way to go forward and backwards between versions of your database


  • We didn’t have to write a single line of data access code





  • …here’s some more strange Ruby syntax


  • this helper method generates the html for a link
  • this helper method generates the html for a link






  • …this generates a number of helpful routes for us









  • ...since for most of you this is your first time seeing Ruby code, let’s look at this a little bit
  • params[:id] comes from resources route



  • ActiveRecord is....
    Default option, but you can use any ORM that implements ActiveModel
  • We still haven’t written a single line of data access code





  • …here’s some more strange Ruby syntax






  • …this generates a number of helpful routes for us



  • ...since for most of you this is your first time seeing Ruby code, let’s look at this a little bit



  • ActiveRecord is....
    Default option, but you can use any ORM that implements ActiveModel
  • We still haven’t written a single line of data access code





  • With builder all of our block parameters become parent XML tags and all of our method calls child tags; we define attributes by passing a hash to a tag method

    …what if we wanted to enable easy Javascript interaction?


  • …this generates a number of helpful routes for us



  • ...since for most of you this is your first time seeing Ruby code, let’s look at this a little bit



  • ActiveRecord is....
    Default option, but you can use any ORM that implements ActiveModel
  • We still haven’t written a single line of data access code





  • respond_with tells ActionController to format the object if it can

    Rails can convert any object into JSON

  • We’ve barely scratched the surface of what Rails can do.
  • Convention is very powerful, but sometimes it makes sense to override the defaults
  • Rails gets most of its flexibility from the Ruby language. Uses “metaprogramming” to generate code at runtime. Optimized for the best of productivity & performance.
  • You’d be surprised to see who’s using Rails.
  • Highly recommend some further reading to get a better feel for how you can make Rails work for you.



  • ...in case you missed it


Transcript

  • 1. density!"! Ruby on Rails An Historical Introduction Cleveland Web Sig June 19, 2010
  • 2. Overview
  • 3. Joe Fiorini Rails Developer, Technology Evangelist, Husband, Dog Lover
  • 4. density!"!
  • 5. Goals Why to use Rails? What’s Ruby and what’s Rails? How does Rails make developers happy? Where do I go for more information?
  • 6. Assumptions
  • 7. Assumptions Basic HTTP knowledge
  • 8. Assumptions Basic HTTP knowledge Basic object-oriented principles
  • 9. Assumptions Basic HTTP knowledge Basic object-oriented principles Dynamic web development
  • 10. Assumptions Basic HTTP knowledge Basic object-oriented principles Dynamic web development SQL
  • 11. #websigrails For the Twits
  • 12. Core Philosophy
  • 13. DRY Don’t Repeat Yourself
  • 14. Convention Over Configuration Get Started Faster!
  • 15. REST Your Application as an API
  • 16. MVC
  • 17. Model Data Tier
  • 18. View Displayed to the user
  • 19. Controller Tying the model and view together
  • 20. “ I'm trying to make Ruby natural, not simple. Yukihiro “Matz” Matsumoto
  • 21. ActionPack ActiveModel ActionMailer ActiveResource ActiveSupport Railties
  • 22. ActionPack ActiveModel ActionMailer ActiveResource ActiveSupport Railties
  • 23. ActionPack ActiveModel ActionMailer ActiveResource ActiveSupport Railties
  • 24. ActionPack ActiveModel ActionMailer ActiveResource ActiveSupport Railties
  • 25. ActionPack ActiveModel ActionMailer ActiveResource ActiveSupport Railties
  • 26. ActionPack ActiveModel ActionMailer ActiveResource ActiveSupport Railties
  • 27. ActionPack ActiveModel ActionMailer ActiveResource ActiveSupport Railties
  • 28. ActionPack ActiveModel ActionMailer ActiveResource ActiveSupport Railties
  • 29. Life of a Request
  • 30. http://www.densitypop.com/
  • 31. ActionPack ActionDispatch
  • 32. ActionPack Blog3::Application.routes.draw do |map| root :to => "posts#index" end
  • 33. ActionPack ActionDispatch
  • 34. ActionPack ActionDispatch
  • 35. ActionPack ActionDispatch ActionController
  • 36. ActionPack ActionDispatch class PostsController < ApplicationController def index @posts = Post.order("created_at DESC"). limit(10) end end
  • 37. ActionPack ActionDispatch Inheritence class PostsController < ApplicationController def index @posts = Post.order("created_at DESC"). limit(10) end end
  • 38. ActionPack ActionDispatch Instance Method class PostsController < ApplicationController def index @posts = Post.order("created_at DESC"). limit(10) end end
  • 39. ActionPack ActionDispatch Instance Variable (field/private variable) class PostsController < ApplicationController def index @posts = Post.order("created_at DESC"). limit(10) end end
  • 40. ActionPack ActionDispatch Class Class method (static) class PostsController < ApplicationController def index @posts = Post.order("created_at DESC"). limit(10) end end
  • 41. ActionPack ActionDispatch ActionController
  • 42. ActionPack ActionDispatch ActionController
  • 43. ActiveModel ActionDispatch ActionController ActiveRecord
  • 44. ActiveRecord SQLite/ MySQL/ PostgreSQL/…
  • 45. ActiveRecord SQLite/ MySQL/ MongoDB/… posts id title body
  • 46. ActiveRecord SQLite/ MySQL/ MongoDB/… posts id Post title body
  • 47. ActiveRecord class Post < ActiveRecord::Base end
  • 48. ActiveRecord post.title post.body post.id post.save post.destroy
  • 49. ActiveRecord Post.find_by_title "Awesomest post ever!" SELECT * FROM posts WHERE title = 'Awesomest post ever!'
  • 50. ActiveRecord Post.where("title = 'Awesomest post ever!'") SELECT * FROM posts WHERE title = 'Awesomest post ever!'
  • 51. ActiveRecord Post.joins(:authors).where("authors.name = 'Joe'") SELECT * FROM posts INNER JOIN authors ON posts.author_id = authors.id WHERE authors.author_name = 'Joe'
  • 52. ActiveRecord Post.find_by_title "Awesomest post ever!" vs. Post.where("title = 'Awesomest post ever!'")
  • 53. ActiveRecord class CreatePosts < ActiveRecord::Migration def self.up create_table :posts do |t| t.string :title t.text :body t.timestamps end end def self.down drop_table :posts end end
  • 54. ActiveRecord Validations validates_presence_of validates_uniqueness_of validates_format_of validates_length_of …
  • 55. ActiveRecord http://guides.rails.info RTFM
  • 56. ActiveModel ActionDispatch ActionController class Post < ActiveRecord::Base end
  • 57. ActiveModel ActionDispatch ActionController ActiveRecord
  • 58. ActiveModel ActionDispatch ActionController ActiveRecord
  • 59. ActionPack ActionDispatch ActionController ActiveRecord ActionView
  • 60. ActionPack ActionDispatch ActionController ActiveRecord <ol> <% @posts.each do |post| %> <li class="post"> <h2><%= link_to post.title, post %></h2> <span><%= post.body %></span> </li> <% end %> </ol>
  • 61. ActionPack ActionDispatch Iterator (for loop) ActionController ActiveRecord <ol> <% @posts.each do |post| %> <li class="post"> <h2><%= link_to post.title, post %></h2> <span><%= post.body %></span> </li> <% end %> </ol>
  • 62. ActionPack Block (anonymous function) ActionDispatch ActionController ActiveRecord Block parameter <ol> <% @posts.each do |post| %> do |post| %> <li class="post"> <h2><%= link_to post.title, post %></h2> <span><%= post.body %></span> </li> <% end %> <% end %> </ol>
  • 63. ActionPack ActionDispatch ActionController ActiveRecord Helper method <ol> <% @posts.each do |post| %> <li class="post"> <h2> </h2> <span><%= post.body %></span> %> <%= link_to post.title, post </li> <% end %> </ol>
  • 64. ActionPack ActionDispatch ActionController ActiveRecord Helper method <ol> <% @posts.each do |post| %> <li class="post"> <h2> </h2> <span><%= post.body %></span> %> <%= link_to post.title, post </li> <% end %> <a href="...">Post Title</a> </ol>
  • 65. ActionPack ActionDispatch ActionController ActiveRecord ActionView
  • 66. ActionPack ActionDispatch ActionController ActionView ActiveRecord <html> ... </html>
  • 67. http://densitypop.com/posts/22
  • 68. ActionPack ActionDispatch
  • 69. ActionPack Blog3::Application.routes.draw do |map| resources :posts end
  • 70. GET /posts/new Post#new
  • 71. POST /posts Post#create
  • 72. GET /posts/edit/22 Post#edit
  • 73. PUT /post/22 Post#update
  • 74. GET /posts/22 Post#show
  • 75. DELETE /posts/22 Post#destroy
  • 76. ActionPack ActionDispatch
  • 77. ActionPack ActionDispatch
  • 78. ActionPack ActionDispatch ActionController
  • 79. ActionPack ActionDispatch class PostsController < ApplicationController def show @post = Post.find(params[:id]) end end
  • 80. ActionPack ActionDispatch Request Parameter Hash class PostsController < ApplicationController def show @post = Post.find( ) end params[:id] end
  • 81. ActionPack ActionDispatch ActionController
  • 82. ActionPack ActionDispatch ActionController
  • 83. ActiveModel ActionDispatch ActionController ActiveRecord
  • 84. ActiveModel ActionDispatch ActionController class Post < ActiveRecord::Base end
  • 85. ActiveModel ActionDispatch ActionController ActiveRecord
  • 86. ActiveModel ActionDispatch ActionController ActiveRecord
  • 87. ActionPack ActionDispatch ActionController ActiveRecord ActionView
  • 88. ActionPack ActionDispatch ActionController ActiveRecord <h1><%= link_to @post.title, @post %></h1> <span><%= @post.body %></span>
  • 89. ActionPack ActionDispatch ActionController ActiveRecord ActionView
  • 90. ActionPack ActionDispatch ActionController ActionView ActiveRecord <html> ... </html>
  • 91. http://densitypop.com/feed.xml
  • 92. ActionPack ActionDispatch
  • 93. ActionPack Blog3::Application.routes.draw do |map| match 'feed.xml', :to => 'posts#index' end
  • 94. ActionPack ActionDispatch
  • 95. ActionPack ActionDispatch
  • 96. ActionPack ActionDispatch ActionController
  • 97. ActionPack ActionDispatch class PostsController < ApplicationController respond_to :xml def index respond_with(@posts = Post. order("created_at DESC"). limit(10)) end end
  • 98. ActionPack ActionDispatch ActionController
  • 99. ActionPack ActionDispatch ActionController
  • 100. ActiveModel ActionDispatch ActionController ActiveRecord
  • 101. ActiveModel ActionDispatch ActionController class Post < ActiveRecord::Base end
  • 102. ActiveModel ActionDispatch ActionController ActiveRecord
  • 103. ActiveModel ActionDispatch ActionController ActiveRecord
  • 104. ActionPack ActionDispatch ActionController ActiveRecord ActionView
  • 105. ActionPack ActionDispatch ActionController ActiveRecord atom_feed do |feed| feed.title("Joe's Awesome Blog!") feed.updated(@posts.first.created_at) @posts.each do |post| feed.entry(post) do |entry| entry.title(post.title) entry.content(post.body, :type => 'html') entry.author { |author| author.name("Joe") } end end end
  • 106. http://densitypop.com/posts.json
  • 107. ActionPack ActionDispatch
  • 108. ActionPack Blog3::Application.routes.draw do |map| resources :posts end
  • 109. ActionPack ActionDispatch
  • 110. ActionPack ActionDispatch
  • 111. ActionPack ActionDispatch ActionController
  • 112. ActionPack ActionDispatch class PostsController < ApplicationController respond_to :xml, :json def index respond_with(@posts = Post. order("created_at DESC"). limit(10)) end end
  • 113. ActionPack ActionDispatch ActionController
  • 114. ActionPack ActionDispatch ActionController
  • 115. ActiveModel ActionDispatch ActionController ActiveRecord
  • 116. ActiveModel ActionDispatch ActionController class Post < ActiveRecord::Base end
  • 117. ActiveModel ActionDispatch ActionController ActiveRecord
  • 118. ActiveModel ActionDispatch ActionController ActiveRecord
  • 119. ActionPack ActionDispatch ActionController ActiveRecord ActionView
  • 120. ActionPack ActionDispatch ActionController ActiveRecord [This slide intentionally left blank]
  • 121. There you go. Isn’t that easy?
  • 122. Digging Deeper
  • 123. Nothing is Sacred Anything can be overridden
  • 124. Not all magic Some illusions too
  • 125. Enterprise Ready http://www.workingwithrails.com/high-profile-organisations
  • 126. http://bit.ly/websigrailslinks
  • 127. Resources http://bit.ly/websigrailslinks
  • 128. Cleveland Ruby Brigade http://www.meetup.com/ClevelandRuby http://bit.ly/websigrailslinks
  • 129. Agile Web Dev. with Rails http://pragprog.com/titles/rails4/ http://bit.ly/websigrailslinks
  • 130. Rails 3 in Action http://www.manning.com/katz/ http://bit.ly/websigrailslinks
  • 131. http://bit.ly/websigrailslinks
  • 132. The End
  • 133. http://speakerrate.com/joefiorini Please rate this talk (only if it’s good)!