SlideShare a Scribd company logo
Fitting for the occasion


      MeetUP @ Balabit



         October 14, 2010
         nucc@balabit.com
Rails 3.0

• Merb + Rails 2.3 = Rails 3.0
• Bundler
• Rack
• HTML 5
• Arel
Rails 3.0

 • Merb + Rails 2.3 = Rails 3.0
users                                 
  .where(users[:name].eq('nucc'))       
 • Bundler
  .project(users[:id])                
# => SELECT users.id FROM users WHERE users.name = 'nucc'
 • Rack
users.where(users[:name].eq('bob')).where(users[:age].lt(25))
#•=> SELECT * FROM users where users.name=’bob’ and users.age < 25
   HTML 5
 • Arel
users.where(users[:name].eq('bob').or(users[:age].lt(25)))
# => SELECT * FROM users where users.name=’bob’ or users.age < 25

valid_users = users.where(users[:valid].eq(1))
men = valid_users.where(users[:sex].eq(1))
women = valid_users.where(users[:sex].eq(2))
number_of_women = women.count()
Sharing information

  Product   Products
  Model     Controller
Sharing information

  Product   Products
  Model     Controller
REST

• Representational State Transfer

• Roy Fielding - 2000
• SOAP is a protocol, REST is an architecture
• Representation of a resource
• iWiW, Facebook, GitHub, Twitter, Google Search
REST Mapping

    Verb          Path           Action
    GET        /products         index
    GET       /products/:id      show
    POST       /products         create
    GET     /products/:id/edit    edit
    PUT       /products/:id      update
   DELETE     /products/:id      delete
Configuration
 /app/models/product.rb                     /app/controllers/products_controller.rb
 class Product < ActiveRecord::Base         class Products < ApplicationController
 end
                                             def index
                                              @products = Product.all
                                             end

                                             def show
                                              @product = Product.find params[:id]
                                             end


 config/routes.rb                             def update
                                                ...
 Meetup::Application.routes.draw do |map|
                                             end
  resources :products
                                             ...
 end
                                            end
Using resource
/app/models/user.rb
class User < ActiveResource::Base
   site.url = “http://api.twitter.com”
end

                                         /app/controller/user_controller.rb
                                         class UserController < ActionController::Base
                                            self.site = “http://api.twitter.com”

                                           self.element_name = “user”
                                           self.proxy = “...”
                                           self.timeout = 5
                                           self.ssl_options = { :key => “...” }
                                         end
Fitting for the occasion

  Product   Product
  Model     Controller
Fitting for the occasion

                            HTTP
  Product   Product
  Model     Controller
                                Android UI / XML

                                   iPhone UI / XML


                                XML

                         JSON
Respond to
class ProductsController < Application
  def index
   @products = Product.all

  respond_to do |format|
    format.html                 # index.html.erb
    format.iphone               # index.iphone.erb
    format.json { render :json => @products }
    format.xml { render :xml => @products}
  end
 end
end
Fitting for the occasion

            Product    /products.html
  Product
  Model     Controller
                                 /products.xml

                                  /products.iphone


                           /products.rss

                  /products.js
Review


         /app/models
         /app/controllers


         /app/views
         /app/helpers
Rendering
render :xml => @products
render :json => @products
render :file => “../owners/owner.html.erb”
render :text => “Hello World”


render :update do |page|
 page.replace_html(“products”, :partial => product, :object=> @product)
end
Views
                                Layout
                :header

                     content




                                         index.html.erb
        :menu      product #1

                   product #2

                   product #3


                :footer
Layout
 # layout.html.erb           # index.html.erb
 <html>
  <body>                     <%= content_for :header do %>
   <div class=”header”>        <span> BALABIT Products </span>
     <%= yield :header %>    <% end %>
   </div>
   <div class=”content”>     <%= content_for :menu do %>
     <div class=”menu”>        <li>Special menu item</li>
        <%= yield :menu %>   <% end %>
     </div>
   </div>
                             <div class=”products”>
   <div class=”content”>       ...
    <%= yield %>             </div>
   </div>

    ...
Partial

/app/views/products/index.html.erb
            content              #object
                                 render :partial => “product”, :object => ...
          product #1
                                 #array
                                 render :partial => “product”, :collection => ...
          product #2

          product #3


                         /app/views/products/_product.html.erb
Partial
                             # /app/views/products/index.html.erb
                             <div class=”products”>
                              <%= render :partial => “product”, :collection => @products %>
/app/views/products/index.html.erb
                             </div>

            content          # /app/views/products/_product.html.erb
                                   #object
                             <div class=”product”>
                               <span class=”title”><%= product.name %></span> =>
                                    render :partial => “product”, :object             ...
          product #1           <span class=”owner”><%= product.owner %></span>
                             </div>#array
                                  render :partial => “product”, :collection => ...
          product #2         # result
                             <div class=”products”>
                              <div class=”product”>
          product #3             <span class=”title”>SCB</span>
                                 <span class=”owner”>Marci</span>
                              </div>
                             </div>
                         /app/views/products/_product.html.erb
Helpers

/app/views/products/index.html.erb                  /app/helpers/products_helper.rb
<div>                                               module ProductsHelper
  <span> Number of products: </span>
  <%= number_of_products(@product) %>                def number_of_products( products )
<div>                                                 products.select{ |p| p.active? }.size
                                                     end
<% form_for @product, :remote => true do | f | %>
 <%= f.text_field :version %>                        end
<% end %>
Cache
class ProductController < Application   class ProductController < Application
                                           before_filter :authentication
  caches_page :index                       caches_action :index

  def index                               def index
   @products = Product.all                 @products = Product.all
  end                                     end

    def create                              def create
      expire_page :action => :index           expire_page :action => :index
      ...                                     ...
    end                                     end
 ...                                     ...
end                                     end
Cache
<html>
 <body>

<% cache do %>
  <div class=”menu”>
   <li>Show products</li>
  </div>
<% end %>

<% cache(:action => 'update', :action_suffix => 'products') do %>
  <div class=”products”>
    <%= render :partial => “product”, :collection => @products %>
  </div>
<% end %>

# expire_fragment( :controller => ‘products’, :action => 'index', :action_suffix => 'products')
Questions?
Thank you!

More Related Content

PPTX
Testing C# and ASP.net using Ruby
PDF
ParisJS #10 : RequireJS
PPTX
DJango admin interface
PDF
Advanced RESTful Rails
PPTX
AngularJs-training
KEY
Django Admin: Widgetry & Witchery
PDF
Gae Meets Django
PDF
Rails MVC by Sergiy Koshovyi
Testing C# and ASP.net using Ruby
ParisJS #10 : RequireJS
DJango admin interface
Advanced RESTful Rails
AngularJs-training
Django Admin: Widgetry & Witchery
Gae Meets Django
Rails MVC by Sergiy Koshovyi

What's hot (20)

KEY
Single Page Web Apps with Backbone.js and Rails
PDF
Get AngularJS Started!
PDF
Solid angular
PPTX
Magento Indexes
PPTX
Fixing Magento Core for Better Performance - Ivan Chepurnyi
PDF
Best Practices for Magento Debugging
PDF
Applications: A Series of States
PDF
Ajax on drupal the right way - DrupalCamp Campinas, São Paulo, Brazil 2016
PDF
devise tutorial - 2011 rubyconf taiwan
PPTX
Editing the Visual Editor (WordPress)
PDF
Styling recipes for Angular components
PDF
How to disassemble one monster app into an ecosystem of 30
PDF
[FEConf Korea 2017]Angular 컴포넌트 대화법
PPTX
Cape Town MS Developer User Group: Xamarin Community Toolkit
PDF
MVVM with SwiftUI and Combine
PPTX
ChocolateChip-UI
PDF
Why SOLID matters - even for JavaScript
PDF
Angular JS Routing
PDF
Dependency Management with RequireJS
PDF
날로 먹는 Django admin 활용
Single Page Web Apps with Backbone.js and Rails
Get AngularJS Started!
Solid angular
Magento Indexes
Fixing Magento Core for Better Performance - Ivan Chepurnyi
Best Practices for Magento Debugging
Applications: A Series of States
Ajax on drupal the right way - DrupalCamp Campinas, São Paulo, Brazil 2016
devise tutorial - 2011 rubyconf taiwan
Editing the Visual Editor (WordPress)
Styling recipes for Angular components
How to disassemble one monster app into an ecosystem of 30
[FEConf Korea 2017]Angular 컴포넌트 대화법
Cape Town MS Developer User Group: Xamarin Community Toolkit
MVVM with SwiftUI and Combine
ChocolateChip-UI
Why SOLID matters - even for JavaScript
Angular JS Routing
Dependency Management with RequireJS
날로 먹는 Django admin 활용
Ad

Viewers also liked (8)

PDF
Rails Models
PDF
Have2do.it
PDF
Open Academy - Ruby
PDF
Git thinking
PDF
Test Driven Development
PDF
Shade műhely
PDF
Ruby gems
PDF
Munkafolyamatok modellezése OPM segítségével
Rails Models
Have2do.it
Open Academy - Ruby
Git thinking
Test Driven Development
Shade műhely
Ruby gems
Munkafolyamatok modellezése OPM segítségével
Ad

Similar to Resource and view (20)

PDF
The Rails Way
PDF
Simple restfull app_s
PDF
Ride on the Fast Track of Web with Ruby on Rails- Part 2
PDF
Ruby on Rails : RESTful 和 Ajax
PDF
Rails 3: Dashing to the Finish
PDF
Introduction à Ruby
PDF
WebcampZG - Rails 4
PDF
Angular.js Fundamentals
PDF
Rails for Beginners - Le Wagon
PDF
Rails 3 overview
PDF
实战Ecos
PPTX
Templates, partials and layouts
KEY
Rails Antipatterns | Open Session with Chad Pytel
PDF
Rails antipatterns
PDF
Rails antipattern-public
KEY
Código Saudável => Programador Feliz - Rs on Rails 2010
ZIP
Presentation.Key
PDF
What's new in Rails 4
PDF
AngularJS vs. Ember.js vs. Backbone.js
PDF
Intro to-rails-webperf
The Rails Way
Simple restfull app_s
Ride on the Fast Track of Web with Ruby on Rails- Part 2
Ruby on Rails : RESTful 和 Ajax
Rails 3: Dashing to the Finish
Introduction à Ruby
WebcampZG - Rails 4
Angular.js Fundamentals
Rails for Beginners - Le Wagon
Rails 3 overview
实战Ecos
Templates, partials and layouts
Rails Antipatterns | Open Session with Chad Pytel
Rails antipatterns
Rails antipattern-public
Código Saudável => Programador Feliz - Rs on Rails 2010
Presentation.Key
What's new in Rails 4
AngularJS vs. Ember.js vs. Backbone.js
Intro to-rails-webperf

Recently uploaded (20)

PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Advanced Soft Computing BINUS July 2025.pdf
PDF
GamePlan Trading System Review: Professional Trader's Honest Take
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
cuic standard and advanced reporting.pdf
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
[발표본] 너의 과제는 클라우드에 있어_KTDS_김동현_20250524.pdf
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPT
Teaching material agriculture food technology
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
NewMind AI Monthly Chronicles - July 2025
NewMind AI Weekly Chronicles - August'25 Week I
Dropbox Q2 2025 Financial Results & Investor Presentation
Advanced Soft Computing BINUS July 2025.pdf
GamePlan Trading System Review: Professional Trader's Honest Take
Network Security Unit 5.pdf for BCA BBA.
CIFDAQ's Market Insight: SEC Turns Pro Crypto
cuic standard and advanced reporting.pdf
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
[발표본] 너의 과제는 클라우드에 있어_KTDS_김동현_20250524.pdf
MYSQL Presentation for SQL database connectivity
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Teaching material agriculture food technology
Understanding_Digital_Forensics_Presentation.pptx
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...

Resource and view

  • 1. Fitting for the occasion MeetUP @ Balabit October 14, 2010 nucc@balabit.com
  • 2. Rails 3.0 • Merb + Rails 2.3 = Rails 3.0 • Bundler • Rack • HTML 5 • Arel
  • 3. Rails 3.0 • Merb + Rails 2.3 = Rails 3.0 users .where(users[:name].eq('nucc')) • Bundler .project(users[:id]) # => SELECT users.id FROM users WHERE users.name = 'nucc' • Rack users.where(users[:name].eq('bob')).where(users[:age].lt(25)) #•=> SELECT * FROM users where users.name=’bob’ and users.age < 25 HTML 5 • Arel users.where(users[:name].eq('bob').or(users[:age].lt(25))) # => SELECT * FROM users where users.name=’bob’ or users.age < 25 valid_users = users.where(users[:valid].eq(1)) men = valid_users.where(users[:sex].eq(1)) women = valid_users.where(users[:sex].eq(2)) number_of_women = women.count()
  • 4. Sharing information Product Products Model Controller
  • 5. Sharing information Product Products Model Controller
  • 6. REST • Representational State Transfer • Roy Fielding - 2000 • SOAP is a protocol, REST is an architecture • Representation of a resource • iWiW, Facebook, GitHub, Twitter, Google Search
  • 7. REST Mapping Verb Path Action GET /products index GET /products/:id show POST /products create GET /products/:id/edit edit PUT /products/:id update DELETE /products/:id delete
  • 8. Configuration /app/models/product.rb /app/controllers/products_controller.rb class Product < ActiveRecord::Base class Products < ApplicationController end def index @products = Product.all end def show @product = Product.find params[:id] end config/routes.rb def update ... Meetup::Application.routes.draw do |map| end resources :products ... end end
  • 9. Using resource /app/models/user.rb class User < ActiveResource::Base site.url = “http://api.twitter.com” end /app/controller/user_controller.rb class UserController < ActionController::Base self.site = “http://api.twitter.com” self.element_name = “user” self.proxy = “...” self.timeout = 5 self.ssl_options = { :key => “...” } end
  • 10. Fitting for the occasion Product Product Model Controller
  • 11. Fitting for the occasion HTTP Product Product Model Controller Android UI / XML iPhone UI / XML XML JSON
  • 12. Respond to class ProductsController < Application def index @products = Product.all respond_to do |format| format.html # index.html.erb format.iphone # index.iphone.erb format.json { render :json => @products } format.xml { render :xml => @products} end end end
  • 13. Fitting for the occasion Product /products.html Product Model Controller /products.xml /products.iphone /products.rss /products.js
  • 14. Review /app/models /app/controllers /app/views /app/helpers
  • 15. Rendering render :xml => @products render :json => @products render :file => “../owners/owner.html.erb” render :text => “Hello World” render :update do |page| page.replace_html(“products”, :partial => product, :object=> @product) end
  • 16. Views Layout :header content index.html.erb :menu product #1 product #2 product #3 :footer
  • 17. Layout # layout.html.erb # index.html.erb <html> <body> <%= content_for :header do %> <div class=”header”> <span> BALABIT Products </span> <%= yield :header %> <% end %> </div> <div class=”content”> <%= content_for :menu do %> <div class=”menu”> <li>Special menu item</li> <%= yield :menu %> <% end %> </div> </div> <div class=”products”> <div class=”content”> ... <%= yield %> </div> </div> ...
  • 18. Partial /app/views/products/index.html.erb content #object render :partial => “product”, :object => ... product #1 #array render :partial => “product”, :collection => ... product #2 product #3 /app/views/products/_product.html.erb
  • 19. Partial # /app/views/products/index.html.erb <div class=”products”> <%= render :partial => “product”, :collection => @products %> /app/views/products/index.html.erb </div> content # /app/views/products/_product.html.erb #object <div class=”product”> <span class=”title”><%= product.name %></span> => render :partial => “product”, :object ... product #1 <span class=”owner”><%= product.owner %></span> </div>#array render :partial => “product”, :collection => ... product #2 # result <div class=”products”> <div class=”product”> product #3 <span class=”title”>SCB</span> <span class=”owner”>Marci</span> </div> </div> /app/views/products/_product.html.erb
  • 20. Helpers /app/views/products/index.html.erb /app/helpers/products_helper.rb <div> module ProductsHelper <span> Number of products: </span> <%= number_of_products(@product) %> def number_of_products( products ) <div> products.select{ |p| p.active? }.size end <% form_for @product, :remote => true do | f | %> <%= f.text_field :version %> end <% end %>
  • 21. Cache class ProductController < Application class ProductController < Application before_filter :authentication caches_page :index caches_action :index def index def index @products = Product.all @products = Product.all end end def create def create expire_page :action => :index expire_page :action => :index ... ... end end ... ... end end
  • 22. Cache <html> <body> <% cache do %> <div class=”menu”> <li>Show products</li> </div> <% end %> <% cache(:action => 'update', :action_suffix => 'products') do %> <div class=”products”> <%= render :partial => “product”, :collection => @products %> </div> <% end %> # expire_fragment( :controller => ‘products’, :action => 'index', :action_suffix => 'products')