rubybook = Blog.new :title => “Gerald Bauer’s Ruby Notebook”, :about => “Internet Professional in Vancouver, B.C.”

Gerald Bauer’s Ruby Notebook

Let’s Go Camping - Getting Started w/ Ruby’s Model-View-Controller (MVC) Web Microframework

What’s Camping? Camping is a microframework (in less than 4k of code) that lets you develop a web application using the Model-View-Controller (MVC) architecture with all your code stored in a single Ruby script in less then three minutes.

Let’s go camping and create a web application (hello.rb) showing the current time:

require 'camping'

Camping.goes :Hello

module Hello::Controllers
  class Index < R '/'
    def get
      render :frontpage
    end
  end
end

module Hello::Views
  def frontpage
    h1 "Hola Mundo"
    p  "The time is: #{Time.now}"
  end
end

We have created a controller with an index action mapped to the ‘/’ route and a frontpage view template and that’s it! We’re ready for production.

Let’s use the Mongrel web server to put our Camping web application online. Add the following startup script to hello.rb:

if __FILE__ == $0
  require 'mongrel'
  require 'mongrel/camping'

  server = Mongrel::Camping::start( "0.0.0.0", 3301, "/", Hello )
  puts "**Hello Camping is running on Mongrel at http://localhost:3301/"
  server.run.join
end

Start up the web application and the Mongrel web server and type on the command line:

$ ruby hello.rb
>> Hello Camping is running on Mongrel at http://localhost:3301/

Tune your browser to http://localhost:3301 Voila!

Note, you can grab the sources for this article from the Vancouver.rb repository.

Any comments, corrections or additions about “Let’s Go Camping – Getting Started w/ Ruby’s Model-View-Controller (MVC) Web Microframework”? Send them along to the Vancouver.rb Mailing List/Forum. Thanks!

Written by Gerald

January 13, 2008 at 9:57 pm