• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
merb.intro
 

merb.intro

on

  • 8,042 views

An introduction the Merb Framework

An introduction the Merb Framework

Statistics

Views

Total Views
8,042
Views on SlideShare
7,884
Embed Views
158

Actions

Likes
11
Downloads
140
Comments
2

3 Embeds 158

http://paulbarry.com 141
http://www.paulbarry.com 13
http://www.slideshare.net 4

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel

12 of 2 previous next

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • Thanks for posting this. Its the best merb basics walkthrough i've seen yet - very helpful.
    Are you sure you want to
    Your message goes here
    Processing…
  • Should be app/views/articles/show.html.erb, yeah?
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    merb.intro merb.intro Presentation Transcript

    • merb.intro :by => "Paul Barry" "Paul Barry"
    • What is Merb? A Ruby MVC Web Framework similar to Rails, but: • Small • Fast • Light • Less Opinionated
    • Create Your App $ merb-gen app blog ~/projects
    • Configure Your App config/init.rb use_orm :activerecord dependency "merb-action-args" dependency "merb-assets" dependency "merb-builder" dependency "merb-parts" dependency "merb_activerecord" dependency "merb_helpers"
    • Configure Your Database config/database.yml development: adapter: sqlite3 database: dev.db timeout: 5000 test: adapter: sqlite3 database: test.db timeout: 5000
    • Generate a Model $ merb-gen model article title:string body:text ~/projects/blog
    • Create the Table $ rake db:migrate ~/projects/blog
    • Create an Article $ merb -i >> article = Article.new(:title => 'First Post')‏ => #<Article id: nil...> >> article.body = 'Lorem ipsum dolor sit amet...' => &quot;Lorem ipsum dolor sit amet...&quot; >> article.save!=> true ~/projects/blog
    • Create a Controller app/controllers/articles.rb class Articles < Application def index &quot;Hello, World!&quot; end end
    • Create RESTful Route config/router.rb Merb :: Router .prepare do | r | r.resources :articles end
    • Start Merb $ merb ~/projects/blog
    • View the controller
    • Named Route config/router.rb r.match( &quot;/sleep/:time&quot; ).to( :controller => &quot;sleeper&quot; , :action => &quot;execute&quot; ).name( :sleeper )‏
    • Blocking Controller app/controllers/sleeper.rb class Sleeper < Application def execute (time = 5 )‏ sleep time.to_i &quot;I slept for #{time} seconds&quot; end end
    • Non-Blocking Controller app/controllers/sleeper.rb class Sleeper < Application def execute (time = 5 )‏ render_deferred do sleep time.to_i &quot;I slept for #{time} seconds&quot; end end end
    • Demo
    • Create a Real Action app/controllers/articles.rb class Articles < Application def index @articles = Article .find( :all , :limit => 5 , :order => &quot;created_at desc&quot; )‏ display @articles end end
    • Create the View app/views/articles/index.html.erb <% throw_content :page_title, &quot;Articles&quot; %><%= partial 'article', :with => @articles %> <% throw_content :page_title , &quot;Articles&quot; %> <% throw_content :sidebar do %> <%= partial 'shared/me' %> <% end %> <%= partial 'article' , :with => @articles %>
    • Create the Partial app/views/articles/_article.html.erb <% throw_content :page_title, &quot;Articles&quot; %><%= partial 'article', :with => @articles %> <div id= &quot;article_ <%= article.id %> &quot; class= &quot;article&quot; > <h2> <%= link_to h(article.title), url( :article , article) %> </h2> <p class= &quot;posted_at&quot; > Posted at <%= article.created_at.strftime &quot;%l:%M %p&quot; %> on <%= article.created_at.strftime &quot;%A, %B %e&quot; %> <p> <%= h(article.body) %> </p> </div>
    • Create the Shared Partial app/views/shared/_me.html.erb <% throw_content :page_title, &quot;Articles&quot; %><%= partial 'article', :with => @articles %> <h2> About Me </h2> <%= image_tag &quot;me.jpg&quot; %>
    • Create the Layout app/views/layout/articles.html.erb <% throw_content :page_title, &quot;Articles&quot; %><%= partial 'article', :with => @articles %> <html> <head> <title> Merb Blog - <%= catch_content :page_title %> </title> <%= css_include_tag 'application' %> <%= catch_content :html_head %> </head> <body> <div id= &quot;page&quot; > <div id= &quot;header&quot; ><h1> Merb Blog </h1></div> <div id= &quot;content&quot; > <%= catch_content %> </div> <div id= &quot;sidebar&quot; > <%= catch_content :sidebar %> </div> </div> </body> </html>
    • View Articles
    • Create the Show Action app/controllers/articles.rb <% throw_content :page_title, &quot;Articles&quot; %><%= partial 'article', :with => @articles %> def show (id)‏ @article = Article .find_by_id(id)‏ raise NotFound unless @article display @article end
    • Create the Show View app/views/show.html.erb <% throw_content :page_title, &quot;Articles&quot; %><%= partial 'article', :with => @articles %> <% throw_content :page_title , h( @article .title) %> <% throw_content :sidebar do %> <%= partial 'shared/me' %> <% end %> <%= partial 'article' , :with => @article %>
    • View Article <% throw_content :page_title, &quot;Articles&quot; %><%= partial 'article', :with => @articles %>
    • Add RSS Mime Type config/init.rb <% throw_content :page_title, &quot;Articles&quot; %><%= partial 'article', :with => @articles %> Merb .add_mime_type :rss , nil , %w[text/xml]
    • Add RSS to Provides app/controllers/articles.rb <% throw_content :page_title, &quot;Articles&quot; %><%= partial 'article', :with => @articles %> class Articles < Application provides :rss def index @articles = Article .find( :all , :limit => 5 , :order => &quot;created_at desc&quot; )‏ display @articles end end
    • Create RSS Builder app/views/articles/index.rss.builder <% throw_content :page_title, &quot;Articles&quot; %><%= partial 'article', :with => @articles %> xml.instruct! :xml , :version => &quot;1.0&quot; xml.rss :version => &quot;2.0&quot; do xml.channel do xml.title &quot;Merb Blog&quot; xml.description &quot;The greatest blog in the world&quot; xml.link &quot; http://merb.blog &quot; @articles .each do | a | xml.item do xml.title a.title xml.description a.body xml.pubDate a.created_at.to_s( :rfc822 )‏ xml.link url( :article , a)‏ xml.guid url( :article , a)‏ end end end end
    • Add Auto-Discovery Link app/views/articles/index.html.erb <% throw_content :page_title, &quot;Articles&quot; %><%= partial 'article', :with => @articles %> <% throw_content :html_head do %> <link rel= &quot;alternate&quot; type= &quot;application/rss+xml&quot; title= &quot;RSS&quot; href= &quot;/articles.rss&quot; /> <% end %>
    • RSS Icon <% throw_content :page_title, &quot;Articles&quot; %><%= partial 'article', :with => @articles %>
    • RSS Feed <% throw_content :page_title, &quot;Articles&quot; %><%= partial 'article', :with => @articles %>
    • Create Recent Article Part <% throw_content :page_title, &quot;Articles&quot; %><%= partial 'article', :with => @articles %>
      • class RecentArticles < Merb::PartController
      • def index
      • @articles = Article .find( :all ,
      • :limit => params[ :limit ],
            • :order => 'created_at desc' )‏
      • render
      • end
      • end
      app/parts/recent_articles.rb
    • Recent Article Part View <% throw_content :page_title, &quot;Articles&quot; %><%= partial 'article', :with => @articles %> app/parts/views/recent_articles/index.html.erb <h2> Recent Articles </h2> <ul> <% @articles .each do | a | %> <li> <%= link_to h(a.title), url( :article , a) %> </li> <% end %> </ul>
    • Add Part to View <% throw_content :page_title, &quot;Articles&quot; %><%= partial 'article', :with => @articles %> app/views/articles/show.html.erb <% throw_content :sidebar do %> <%= partial 'shared/me' %> <%= part RecentArticles => &quot;index&quot; , :limit => 5 %> <% end %>
    • Recent Articles <% throw_content :page_title, &quot;Articles&quot; %><%= partial 'article', :with => @articles %>
    • Generate Admin Controller <% throw_content :page_title, &quot;Articles&quot; %><%= partial 'article', :with => @articles %> ~/projects/blog $ merb-gen resource_controller admin/articles create app/controllers/admin create app/helpers/admin create app/views/admin/articles create app/controllers/admin/articles.rb create app/helpers/admin/articles_helper.rb create app/views/admin/articles/edit.html.erb create app/views/admin/articles/index.html.erb create app/views/admin/articles/new.html.erb create app/views/admin/articles/show.html.erb
    • Add Admin Route <% throw_content :page_title, &quot;Articles&quot; %><%= partial 'article', :with => @articles %> config/router.rb Merb :: Router .prepare do | r | r.namespace :admin do | admin | admin.resources :articles end r.resources :articles end
    • View Routes <% throw_content :page_title, &quot;Articles&quot; %><%= partial 'article', :with => @articles %> ~/projects/blog $ merb -i >> merb.show_routes Named Routes edit_admin_article: /admin/articles/:id/edit delete_admin_article: /admin/articles/:id/delete article: /articles/:id admin_articles: /admin/articles new_article: /articles/new admin_article: /admin/articles/:id edit_article: /articles/:id/edit articles: /articles new_admin_article: /admin/articles/new delete_article: /articles/:id/delete
    • Admin Articles View <% throw_content :page_title, &quot;Articles&quot; %><%= partial 'article', :with => @articles %> app/views/admin/articles/index.html.erb <h2> Articles </h2> <p> <%= link_to &quot;Create New Article&quot; , url( :new_admin_article ) %> <ul> <% @articles .each do | a | %> <li> <%= link_to h(a.title), url( :admin_article , a) %> </li> <% end %> </ul>
    • Admin New Article View <% throw_content :page_title, &quot;Articles&quot; %><%= partial 'article', :with => @articles %> app/views/admin/articles/new.html.erb <h2> New Article </h2> <%= partial 'form' %>
    • Admin Edit Article View <% throw_content :page_title, &quot;Articles&quot; %><%= partial 'article', :with => @articles %> app/views/admin/articles/edit.html.erb <h2> Edit Article </h2> <%= partial 'form' %>
    • Admin Article Form <% throw_content :page_title, &quot;Articles&quot; %><%= partial 'article', :with => @articles %> app/views/admin/articles/_form.html.erb <%= error_messages_for :article %> <% form_for @article , :action => url( :admin_article , @article ) do %> <div class= &quot;field&quot; > <%= text_control :title , :label => 'Title' %> </div> <div class= &quot;field&quot; > <%= text_area_control :body , :rows => 20 , :cols => 80 %> <div> <div class= &quot;buttons&quot; > <%= submit_button 'Save' %> </div> <% end %>
    • Create a New Article <% throw_content :page_title, &quot;Articles&quot; %><%= partial 'article', :with => @articles %>
    • Admin Article View <% throw_content :page_title, &quot;Articles&quot; %><%= partial 'article', :with => @articles %> app/views/admin/articles/show.html.erb <h2> <%=h @article .title %> </h2> <p> <%=h @article .body %> </p> <hr /> <%= link_to &quot;Back to Articles&quot; , url( :admin_articles ) %> | <%= link_to &quot;Edit this Article&quot; , url( :edit_admin_article , @article ) %>
    • So is it worth it?
    • Numbers <% throw_content :page_title, &quot;Articles&quot; %><%= partial 'article', :with => @articles %> min avg max stddev rails GET /articles 37.4 45.7 47.0 2.0 rails GET /articles/1 44.0 45.1 45.8 0.6 merb GET /articles 70.0 71.1 73.0 0.8 merb GET /articles/1 88.0 107.1 110.1 7.2 requests/second
    • Thank You! http://merbivore.com #merb on irc.freenode.net http://mwrc2008.conf reaks.com/02zygmuntowicz.html http://paulbarry.co m Resources