Merb 0.9 on Ubuntu Linux 7.10

The Merb application framework is getting close to a 1.0 release. Merb takes many of the ideas of Ruby on Rails and does them one better. RoR was designed for developers and applications, but has not been the best to run in production. Merb was written from the ground up with performance and scalability. But as a result, its like travelling abroad in that things are similar but not exactly the same as you are used to developing with RoR.

The platform of choice of most rubyists these days is OS X, with its fine unixy-goodness and groovy Textmate editor. Ubuntu Linux provides most of these features for a fraction of the price! Also, since these applications are generally deployed on the Linux OS, it makes sense to know how to get things running on that platform as well. Let’s roll up our sleeves and get to work.

I used Justin’s post as a starting point on what to do. Open up a terminal window and start.

Get the Git

First, we install the git version control system which merb uses. Note the current Debian git package has a minor bug requiring the /etc/mailname file. Create that file with your hostname in it for now.

sudo apt-get install git-corevi /etc/mailname


I assume you have been using Ruby before and have a modern Ruby and RubyGems installed. If not, go do that now. I’ll wait.

Justin recommends having these gems. All seem a good idea, but mongrel erubus and rspec seem more necessary if you are trying to cut down. Merb requires rack, and a ORM like Rails’ ActiveRecord, Sequel, or Datamapper, which it looks like it prefers. Merb is built upon the Rack web server abstraction library.

Like Merb, Datamapper is thread-safe to allow multiple requests being handled. However, it is not yet fully developed like ActiveRecord and may not be as production-ready. If you are porting an application from Rails, you may want to continue using ActiveRecord.

sudo gem install mongrel json json_pure erubis mime-types rspec hpricot mocha rubigen haml markaby mailfactory Ruby2Ruby -ysudo gem install racksudo gem install datamapper -y

Install Merb

Merb is available in gem form, but they may not be as up to date as we would like. So let’s git them from the git repository. If you are having problems, try the http:// protocol instead of the git:// protocol. Merb in divided up into merb-core, merb-more, and merb-plugins in order to let you craft the server with just the parts you need. We need to go into each repository and “install” it in your gems.

mkdir trymerbcd trymerbgit clone git:// clone git:// clone git:// merb-coresudo rake installcd ../merb-moresudo rake install# has link_to, etccd ../merge-assets sudo rake installcd ../../merb-plugins/merb_helpers/sudo rake installcd ../merb_activerecord/sudo rake installcd ../merb_datamapper/sudo rake installcd ../merb_rspec/sudo rake install

Whew! That’s going to get old fast. The upcoming gems will be better.

Merb with PostgreSQL

PostgreSQL is my RDBMS of choice. MySQL would work similarly. Ubuntu needs your PostgreSQL header files, so we load them first.

sudo apt-get install libpq-devsudo gem install do_postgres

Now let’s go roll an app!

The MyBlog Merb Application

To show off our merby goodness, we will build the “hello world” of ruby web applications, the blog.

cd trymerbmerb-gen myblogcd myblogcreatedb myblog_development

The createdb command is for postgresql, please substitute the create database for your db of choice.

Edit config/init.rb file and enable the following configurations.
### Uncomment for DataMapper ORM  use_orm :datamapper  use_test :rspec  ### Add your other dependencies here  dependencies "merb_helpers", "merb-assets"
Now create your /config/database.yml
---  # This is a sample database file for the DataMapper ORM  :development: &defaults    :adapter: postgresql    :database: myblog_development    :username: the_user    :password: secrets    :host: localhost  :test:    <<: *defaults    :database: myblog_test  :production:    <<: *defaults    :database: myblog_production

Merbful Authentication

Optionally, we add in authentication, the “Restful Authentication” plugin port to merb. Read me about here

cd trymerbgit clone git:// restful-authenticationgit checkout -b merbful_authentication origin/merbful_authenticationsudo rake installcd trymerb/myblogmerb-gen authenticated user_model session_controller

Our post resource

Restful merb resource that is. This creates the controller, model, and view. If you are using ActiveRecord, I would also expect a migration. For Datamapper, the columns are defined in the model. Use rake to create the tables from the model. (For Postgres, use the datetime type instead of the timestamp type you would use in Rails.)
merb-gen resource post title:string content:text created_at:datetimerake dm:db:automigrate
Edit config/router.rb to enable these actions:
# RESTful routes  r.resources :posts # Change this for your home page to be available at /  r.match('/').to(:controller => 'posts', :action =>'index')

Starting Merb

Open a new terminal window and start merb from your app directory
cd trymerb/myblogmerb
Now point your brower to http://localhost:4000/ and you should be able to see it. The resource generator does not yet build the ERB files out like Rails’ scaffolding. So we have to write that ourselves.

Note that this is where some of the differences between Rails and Merb shows up. Merb has its own routing using the url() helper. Read more about it here .

url(:posts)       #=> "/posts" url(:new_post) #=> "/posts/new" @post = Post[1]url(:post, @post) #=> "/posts/1"
Edit the app/views/posts/index.html.erb and add in a simple post listing.
<h1>My Blog</h1><% @posts.each do |n| %>  <h2><%= n.title %></h2>  <div2><%= n.content %></div><% end %><div>  <%= link_to "Write new", url(:new_post) %></div>

Edit the app/views/posts/new.html.erb and add in the compose form. Feel free to embellish your HTML. I added the hidden field to remember how to do it if I need to figure it out again.

As you see, the field control helpers are different, but self-explanatory. They were actually the hardest part to figure out since they needed to be installed from the proper repository and enabled in the init.rb file. The source code also shows more of what you need to do.

<h1>Write a new post</h1><%= error_messages_for :post %><% form_for(@post, :action=>url(:posts), :method=>"post") do %>  <%= hidden_field :name=>:wave, :value=>'hello' %>  <%= text_control :title, :label=>"Title", :size=>50,     :maxlength=>250, :style=>"font-weight:bold;" %>  <br /><%= text_area_control :content, :label=>"Rant", :rows=>15, :cols=>50 %>  <br /><%= submit_button  "Save" %><% end %>

That should do it! Enjoy your new merb blog app.

More Things with merb

The Merb console

The merb object has some things worth exploring, like the app object in Rails.
merb -iirb(main):001:0> merb.show_routes


Like with Rails 2.0, call the “debugger” function where you want a breakpoint. and start merb like this:
merb -D

Merb on Thin

You can run the Thin web server with Merb.
sudo gem install thinmerb -a thin


This was my first experiment using Merb on Ubuntu, and I am thrilled that it works so well already. This is not quite a recipe for starting from scratch. Some instructions may be convoluted, unnnecessary, copied here incorrectly, or just plain wrong.

Merb is the exciting new direction for developing ruby applications. Ezra is addressing the shortcomings we have in developing and deploying robust applications in ruby. The Rubinius and Merb projects are doing a lot to make this happen.