Merb Beginner’s Tutorial

WARNING: Merb development is moving fast, and this tutorial is already pretty much OUT OF DATE. I’ll try and have a new updated one soon. Merb 1.0 is coming out mid-October! The guys hacking on Merb are pretty darn amazing.

Installation
This is for the edge version - which I actually recommend right now since merb is still having growing pains.

(avoid using sudo gem install merb, etc, and what the slapp tutorial says. They are out of date and/or broken at the time of this writing)

Create an install location
I like to put my stuff in ~/documents/code

mkdir ~/documents/code
cd ~/documents/code

Install dependencies

gem install erubis rake json_pure rspec rack hpricot mime-types ruby2ruby ParseTree \
  memcache-client templater haml mailfactory sequel sake mongrel libxml-ruby english addressable builder

I had problems with sake when installing merb and datamapper on ubuntu 8.04.1 LTS hardy. Instead I used thor (see further below) and it worked nicely - as long as you installed the dependencies above.


Install Sake

sudo gem install ParseTree -v 2.1.1  # sake at the time of writing requires exactly 2.1.1 of ParseTree gem
sudo gem install ruby2ruby -v 1.1.8 # sake at the time of writing requires exactly 1.1.8 of ruby2ruby exactly
sudo gem install sake

Install Merb Edge
Merb is like your Rails, but much lighter and without all the rules. It’s made up of a series of gems which get installed with the following.

sake -i http://github.com/wycats/merb-more/tree/master%2Ftools%2Fmerb-dev.rake?raw=true
sake merb:clone
sake merb:install:all

Install DataMapper Edge
Datamapper is like ActiveRecord but apparently faster.

sake -i http://github.com/dkubb/dm-dev/tree/master/dm-dev.sake?raw=true
sake dm:clone
cd dm
sake dm:install

Install thor

cd ~/sources
git clone git://github.com/wycats/thor.git
cd thor
rake install

Install merb

cd ~/sources
thor install http://github.com/jackdempsey/merb-thor/tree/master%2Fmerb.thor?raw=true --as=merb
thor merb:clone
cd merb
thor merb:install

Install datamapper

cd ~/sources
thor install http://github.com/jackdempsey/dm-thor/tree/master%2Fdm.thor?raw=true --as=dm
thor dm:clone
cd dm
thor dm:install

Install Miscellaneous stuff you’ll probably need

sudo gem install do_mysql
sudo gem install rspec

Create your first app

cd ~/documents/code/merb
merb-gen app  # to get a list of all the options available
merb-gen app merbeer --testing-framework=rspec --template-engine=erb --orm=datamapper # i really don't see the advantage to haml yet so I choose erb as my template engine. haml in my opinion makes it harder to work with a designer who only knows css and html (as far as I can tell)
cd merbeer
mate .  # assuming you use textmate
rake dm:db:database_yaml

Put the following in the database.yml file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# This is a sample database file for the DataMapper ORM
:development: &defaults
  :adapter: mysql
  :database: merbeer_development
  :username: root
  :password: 
  :host: localhost
  :socket: /tmp/mysql.sock
  :encoding: utf8
 
:test:
  <<: *defaults
  :database: merbeer_test
 
:production:
  <<: *defaults
  :database: merbeer_production
merb-gen resource Beer name:string description:text created_at:datetime
rake dm:db:migrate

Edit apps/models/beer.rb & add property :id, Integer, :serial => true because DataMapper requires you to explicitly declare the primary key. It should look like the following.

1
2
3
4
5
6
7
8
9
class Beer
  include DataMapper::Resource
 
  property :id, Integer, :serial => true
  property :name, String
  property :description, Text
  property :created_at, DateTime
 
end

Create databse and rake migrations

rake dm:db:create
rake dm:db:automigrate

Add routes to router.rb (I have a feeling this will be done automatically once Merb 1.0 arrives this October 08)

1
2
3
4
5
6
7
Merb::Router.prepare do |r|
  # RESTful routes
  # r.resources :posts
  r.resources :beers
 
  r.default_routes
end

Now we can start to spec and build.

Wait, hold on one second. Create the test database and move the migrations over first.

rake MERB_ENV=test dm:db:create
rake MERB_ENV=test dm:db:automigrate
rake spec #output should be 1 example 0 failures and 1 pending. ok good.

Now let’s create our first spec. Edit specs/models/beer_spec.rb

1
2
3
4
5
6
7
8
9
10
require File.join( File.dirname(__FILE__), '..', "spec_helper" )
 
describe Beer do
 
  it "should should be valid when new" do
    beer = Beer.new
    beer.should be  #be_valid has become just plain be as far as I can tell
  end
 
end
rake spec # ok good. it should have passed

to be continued…

Comments

  1. Stephane | September 9, 2008

    Useful write and update of slapp

  2. [...] Merb Beginner’s Tutorial - Notes on how to instal the latest edge bits. [...]

  3. oren | September 10, 2008

    when i type:
    sudo sake -i http://github.com/wycats/merb-more/tree/master%2Ftools%2Fmerb-dev.rake?raw=true

    i get this:
    sudo: sake: command not found

    when i do gem list it looks like sake is installed:
    sake (1.0.15)

    I have ubuntu 8.04 and I sake never works on my machine.
    any idea?

  4. [...] assumes you’ve already got started with merb, and that you are using datamapper as your [...]

  5. Scott | September 12, 2008

    @Oren. I had a problem with sake on ubuntu as well - though it was a different error. Instead I used thor and it worked nicely. Give it a shot. I’ve updated the tutorial accordingly. Good luck.

  6. oren | September 13, 2008

    Thanks for your patience and guidance Scott.

    two issues:
    1. i didn’t have rake so I got it with aptitude.

    2. when i type:
    thor install http://github.com/jackdempsey/merb-thor/tree/master%2Fmerb.thor?raw=true –as=merb

    I get:
    bash: thor: command not found

    (and I have thor 0.9.6)

  7. scott | September 13, 2008

    Oren. I’m not really sure, but it sounds like a deeper issue either with your server setup or your gems. This is my best guess given that you were getting a ‘command not found’ error on sake as well.

    Type this from your bash:
    irb

    # then once in the irb prompt type this:
    require ‘rubygems’
    require ‘thor’

    # it should return true (=> true) for both. exit irb with ‘exit’

    Then, maybe try updating your rubygems:
    sudo gem update
    sudo gem update –system

    Then try running just plain “sudo thor” and see if you get a response.

  8. oren | September 13, 2008

    (i had to install irb with aptitude since i got error when trying to run it)

    irb(main):001:0> require ‘rubygems’
    ArgumentError: wrong number of arguments (0 for 1)
    from (irb):1:in `require’
    from (irb):1
    irb(main):001:0> require ‘thor’
    ArgumentError: wrong number of arguments (0 for 1)
    from (irb):1:in `require’
    from (irb):1

  9. oren | September 13, 2008

    btw, here are all my ruby packages. maybe i use old version of something?
    dpkg -l | grep ruby
    ii libgems-ruby1.8 1.1.1-1~hardy1 libraries to use RubyGems, a package managem
    ii libopenssl-ruby1.8 1.8.6.111-2ubuntu1.1 OpenSSL interface for Ruby 1.8
    ii libreadline-ruby1.8 1.8.6.111-2ubuntu1.1 Readline interface for Ruby 1.8
    ii libruby1.8 1.8.6.111-2ubuntu1.1 Libraries necessary to run Ruby 1.8
    ii rake 0.8.1-3 a ruby build program
    ii ruby 4.1 An interpreter of object-oriented scripting
    ii ruby1.8 1.8.6.111-2ubuntu1.1 Interpreter of object-oriented scripting lan
    ii rubygems 1.1.1-1~hardy1 package management framework for Ruby librar

  10. scott | September 13, 2008

    You’re getting that irb error because I think you copied and pasted require ‘rubygems’. Type it in by hand instead. This blog changes quotation marks into their fancier encoded version ‘

  11. oren | September 13, 2008

    thanks. now i get true for both cases in irb but still get this:

    sudo thor
    sudo: thor: command not found

  12. Scott | September 13, 2008

    That tells me there is something wrong with your ubuntu or gem setup oren. I don’t know that stuff too well though so. You might try starting from scratch using the info on articles.slicehost.com. This is who I use for my ubuntu server, and they have great articles.

  13. oren | September 13, 2008

    i’ll do it. thanks so much for the effort.

  14. [...] (this assumes you’ve already setup merb using this tutorial or something similar) [...]