A BRIEF OVERVIEW OF RUBY ON RAILS

A BRIEF OVERVIEW OF RUBY ON RAILS

By Nicolai Reuschling

37f941943817785d0d2ccce1e63cd476?s=128

Rails Girls Frankfurt

March 15, 2013
Tweet

Transcript

  1. Rails Girls Frankfurt am Main 2013 A BRIEF OVERVIEW OF

    RUBY ON RAILS
  2. WHO AM I? Nicolai Reuschling nicolai@reuschling.name software developer, agile coach

    & lecturer
  3. „MATZ“ Yukihiro Matsumoto inventor, designer & creator of Ruby

  4. „DHH“ David Heinemeier Hansson creator of Ruby on Rails

  5. None
  6. None
  7. None
  8. None
  9. None
  10. RUBY ON RAILS FRAMEWORK implemented in Ruby collection of great

    solutions for common problems highly opinionated golden path leads to high productivity configurable as well (convention over configuration)
  11. PROJECT SCAFFOLD Run in console: rails new rails-girls-weblog-demo

  12. SCAFFOLDING code generator tool generates all necessary files model, views,

    controller routing configuration database migration great and easy way to start and build upon Run in console (one line): rails generate scaffold Post title:string body:text
  13. PROJECT SCAFFOLD

  14. PROJECT SCAFFOLD

  15. PROJECT SCAFFOLD

  16. PROJECT SCAFFOLD

  17. BIRD‘S EYE VIEW View Assets + Internet Routing Controller Model

    Database MVC architecture
  18. BIRD‘S EYE VIEW View Assets + Internet Routing Controller Model

    Database MVC architecture
  19. BIRD‘S EYE VIEW View Assets + Internet Routing Controller Model

    Database MVC architecture
  20. ROUTING too magical and complex to explain in this context

    basically, it decides: what request goes where? splits URI and forwards request to controller, e.g.: /posts/ goes to PostsController /comments/42 goes to CommentsController let‘s assume: it just works!
  21. BIRD‘S EYE VIEW View Assets + Internet Routing Controller Model

    Database MVC architecture
  22. BIRD‘S EYE VIEW View Assets + Internet Routing Controller Model

    Database MVC architecture
  23. CONTROLLER handles HTTP requests and sessions collaborates with models pushes

    information into the views decides, which view and what format to render
  24. CONTROLLER ACTIONS index (collection; list of all available resources) show

    (single resource; displays values) new (form for creating a new resource) create (logic to create a new resource) edit (form for updating an existing resource) update (logic to update an existing resource) destroy (logic to delete an existing resource)
  25. CONTROLLER EXAMPLE 1/2 # GET /posts # GET /posts.json #

    GET /posts.xml def index @posts = Post.all respond_to do |format| format.html # index.html.erb format.json { render json: @posts } format.xml { render xml: @posts } end end
  26. CONTROLLER EXAMPLE 2/2 # GET /posts/1 # GET /posts/1.json #

    GET /posts/1.xml def show @post = Post.find(params[:id]) respond_to do |format| format.html # show.html.erb format.json { render json: @post } format.xml { render xml: @post } end end
  27. CONTROLLER EXAMPLE 2/2 # GET /posts/1 # GET /posts/1.json #

    GET /posts/1.xml def show @post = Post.find(params[:id]) respond_to do |format| format.html # show.html.erb format.json { render json: @post } format.xml { render xml: @post } end end
  28. CONTROLLER EXAMPLE 2/2 # GET /posts/1 # GET /posts/1.json #

    GET /posts/1.xml def show @post = Post.find(params[:id]) respond_to do |format| format.html # show.html.erb format.json { render json: @post } format.xml { render xml: @post } end end
  29. BIRD‘S EYE VIEW View Assets + Internet Routing Controller Model

    Database MVC architecture
  30. BIRD‘S EYE VIEW View Assets + Internet Routing Controller Model

    Database MVC architecture
  31. MODELS implements „business logic“ (your application idea) associates collaborators and

    connects data validates information stores and retrieves information from the database
  32. MODEL EXAMPLE 1/2 class Post < ActiveRecord::Base # attributes attr_accessible

    :body, :title # associations has_many :comments # validations validates :title, presence: true, uniqueness: true, length: { within: 2..100 } validates :body, length: { minimum: 10 } # scopes default_scope order('created_at DESC') end
  33. MODEL EXAMPLE 2/2 class Comment < ActiveRecord::Base # attributes attr_accessible

    :author_name, :body, :post_id # associations belongs_to :post # validations validates :author_name, presence: true, length: { maximum: 100 } validates :body, length: { within: 10..1000 } validates :post, associated: true validates :post_id, presence: true # scopes default_scope order('created_at ASC') end
  34. BIRD‘S EYE VIEW View Assets + Internet Routing Controller Model

    Database MVC architecture
  35. BIRD‘S EYE VIEW View Assets + Internet Routing Controller Model

    Database MVC architecture
  36. MIGRATIONS configuration tool for databases creates/deletes or changes tables may

    be used to change large sets of data
  37. MIGRATION EXAMPLE class CreatePosts < ActiveRecord::Migration def change create_table :posts

    do |t| t.string :title t.text :body t.timestamps end end end Run in console: rake db:migrate
  38. BIRD‘S EYE VIEW View Assets + Internet Routing Controller Model

    Database MVC architecture
  39. BIRD‘S EYE VIEW View Assets + Internet Routing Controller Model

    Database MVC architecture
  40. BIRD‘S EYE VIEW View Assets + Internet Routing Controller Model

    Database MVC architecture
  41. BIRD‘S EYE VIEW View Assets + Internet Routing Controller Model

    Database MVC architecture
  42. VIEWS ready HTML page = layout + template + partials

    mixture of HTML tags plus embedded Ruby (ERB) access to all instance variables (@var) set by controller other formats (e.g. JSON, XML) possible as well rendered view is delivered to browser
  43. VIEW EXAMPLE (INDEX) <h1>My Weblog</h1> <table> <tr> <th>Title</th> <th>Body</th> <th

    colspan="3">Actions</th> </tr> <% @posts.each do |post| %> <tr> <td><%= post.title %></td> <td><%= truncate(post.body, separator: ' ', omission: '… (continued)') %></td> <td><%= link_to 'Show', post %></td> <td><%= link_to 'Edit', edit_post_path(post) %></td> <td><%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' } %></td> </tr> <% end %> </table>
  44. VIEW EXAMPLE (SHOW) <p id="notice"><%= notice %></p> <h1><%= @post.title %></h1>

    <p><%= @post.body %></p> <br /> <h2>Comments</h2> <% @post.comments.each do |comment| %> <h3> <%= comment.author_name %> commented on <%= comment.created_at.to_s(:long) %> </h3> <p><%= comment.body %></p> <% end %>
  45. BIRD‘S EYE VIEW View Assets + Internet Routing Controller Model

    Database MVC architecture
  46. BIRD‘S EYE VIEW View Assets + Internet Routing Controller Model

    Database Javascript, stylesheet, images MVC architecture
  47. BIRD‘S EYE VIEW View Assets + Internet Routing Controller Model

    Database Javascript, stylesheet, images MVC architecture
  48. BIRD‘S EYE VIEW View Assets + Internet Routing Controller Model

    Database Javascript, stylesheet, images MVC architecture
  49. FEED YOUR BRAIN 1/2 Ruby http://ruby-doc.org/ Ruby on Rails API

    (Official Code Documentation) http://api.rubyonrails.org/ Ruby on Rails Guides http://guides.rubyonrails.org/
  50. FEED YOUR BRAIN 2/2 Screencasts http://railscasts.com/ The Pragmatic Bookshelf http://pragprog.com/categories/ruby_and_rails

    Demo Project https://github.com/ncreuschling/rails-girls-weblog-demo
  51. QUESTIONS? ASK THEM! OTHERWISE: HAVE FUN!

  52. QUESTIONS? ASK THEM! OTHERWISE: HAVE FUN! Thanks!

  53. CITATIONS/SOURCES title photo by Jaroen Kransen http://www.flickr.com/photos/jkransen/5231797723/ Matz http://de.wikipedia.org/wiki/Yukihiro_Matsumoto DDH

    http://david.heinemeierhansson.com/