Getting Started with Merb – Bundling Merb with your Application

This is a basic description of how to get up and running with merb, from creating your first application to bundling merb edge with the thor tasks.

Ruby and RubyGems

To get started you will need ruby and rubygems installed. This comes pre-installed on mac OSX. If you are using another operating system, get rubygems from here.

Make sure the version of ruby you have installed in >= 1.8.7.
ruby --version should display something like:
ruby 1.8.7 (2008-08-11 patchlevel 72) [i686-darwin9]

If it is not, you need to upgrade. Rubygems should be at latest version too. You can upgrade rubygems by doing a selfupdate or by running:

sudo gem install rubygems-update

Once this is installed then run:

sudo update_rubygems

This will bring rubygems to the latest version

Make sure you have the following gems installed and they are up to date:

sudo gem install rake rack thor rspec mongrel hoe diff-lcs

Installing Thor

Thor tasks are a great way of managing your merb gem installs. To get started you need to add the merb thor tasks, do this with the following command:

thor install http://merbivore.com/merb.thor

Now you are going to need to install the stack. I recommend installing edge rails for this. Although you will install the stack locally inside each application, for the time being you will need the stack on the system:

sudo thor merb:stack:install --edge

and now install what the edge missed out:

sudo thor merb:source:install merb-gen
sudo thor merb:source:install merb-param-protection
sudo thor merb:source:install merb-exceptions
sudo thor merb:source:install merb_datamapper

Creating your first merb app

To generate your basic app structure type:

merb-gen app myfirstapp

This will generate the basic merb stucture.
Click here to see the output I get.
Now we need to bundle merb edge with this application.

Bundling Merb stack with the app

cd into the directory of your app:

cd myfirstapp
thor merb:stack:install --edge

This will install the basic merb stack from edge. To see a list of all available tasks on your system, type:

thor -T --all

Now we have installed merb stack from edge, we want to make sure we are using a stable version of datamapper.

thor merb:gem:install dm-core
thor merb:gem:install dm-more

should do it!

Other Dependencies

You will also need to bundle mongrel:

thor merb:gem:install mongrel

And a good to have is haml:

thor merb:gem:install haml

If you decide to use haml, make sure your init.rb file reflects this.

Configuring the Database

We also need a database adapter, I use do_mysql, but you can use postgres or sqlite.
To bundle do_mysql type:

thor merb:gem:install do_mysql

Now make sure you database.yml file is correctly configured for mysql. Here’s an example database.yml file for mysql

Running your migrations

First you will need to create the relevant databases. There is a rake task to do this. Take a look at all available rake tasks for this app by typing:

./bin/rake -T

.

To create the databases type:

./bin/rake db:create

And now run the migrations:

./bin/rake db:automigrate

Note we are running the rake tasks from the rake bundled with the application. Other than creating new applications using merb-gen, there is no longer a need to be using the system wide gems, so keep to using each local applications bundled gems instead. This makes deployment of the application seamless as there are no dependencies that need to be installed on the remote server other than ruby and rubygems.

Running the Merb Application

Now we are ready to start running the application, so from within the application type:

./bin/merb

If all is well, you should have a running mongrel on port 4000. Go to http://localhost:4000 and you should see something like this:

Starting up your Merb Application for the First Time

Starting up your Merb Application for the First Time

Follow the Instructions on that page to get started!

No Comments

RSS feed for comments on this post. TrackBack URL

No comments yet.

Leave a comment

Preview: