Upgrade to Pro — share decks privately, control downloads, hide ads and more …

A BRIEF OVERVIEW OF RUBY ON RAILS

A BRIEF OVERVIEW OF RUBY ON RAILS

By Nicolai Reuschling

Rails Girls Frankfurt

March 15, 2013
Tweet

More Decks by Rails Girls Frankfurt

Other Decks in Programming

Transcript

  1. 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)
  2. 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
  3. 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!
  4. CONTROLLER handles HTTP requests and sessions collaborates with models pushes

    information into the views decides, which view and what format to render
  5. 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)
  6. 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
  7. 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
  8. 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
  9. 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
  10. MODELS implements „business logic“ (your application idea) associates collaborators and

    connects data validates information stores and retrieves information from the database
  11. 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
  12. 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
  13. 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
  14. 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
  15. 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>
  16. 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 %>
  17. BIRD‘S EYE VIEW View Assets + Internet Routing Controller Model

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

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

    Database Javascript, stylesheet, images MVC architecture
  20. 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/