Creating a Merb Project that Uses a Database

Install

To install Merb, just do:

% sudo gem install merb

(Note: If you are working from svn trunk, then you can simplify your life by just doing 'rake install')

Creating a Project Directory

After installing Merb, you can now create a project folder.

% merb my_merb_project
% cd my_merb_project

(TODO: Do the same rules apply for project names in merb as in Rails. That is, that the name should be a valid SQL table name.)

Configuration

After creating your project, a few configuration options need to be taken care of. One is 'dependencies' and the other is the optional 'database'. Yes, that is right, the database option is just that, an option.

You will find Merb is less opinionated than other web frameworks. In fact, Merb is quite agnostic.

However, for this setup example, we will be configuring Merb to use a database. And, along with that database, we are going to choose an Object Relational Mapper (ORM).

Configuration with ActiveRecord? ORM

Configuration of Merb to use the ActiveRecord? ORM is simple. Just uncomment the appropiate line in config/dependencies.rb

### Uncomment for ActiveRecord ORM
use_orm :activerecord

and install the corresponding gems if you have not already done so.

% sudo gem install activerecord
% sudo gem install merb_activerecord

Configuration with DataMapper? ORM

Configuration of Merb to use the DataMapper? ORM is just as simple. Just uncomment the appropriate line in config/dependencies.rb to

### Uncomment for DataMapper ORM
use_orm :datamapper

and install the corresponding gems if you have not already done so.

% sudo gem install datamapper
% sudo gem install merb_datamapper

If you have not done so, you will probably need to install the DataObjects::Mysql drivers for datamapper. To do this. do the following:

% cd path-where-datamapper-gem-is-installed
% sudo rake dm:install:mysql

Configuration with Sequel ORM

Configuration of Merb to use the Sequel ORM is just as simple. Just uncomment the appropriate line in config/dependencies.rb to

### Uncomment for Sequel ORM
use_orm :sequel

and install the corresponding gems if you have not already done so.

% sudo gem install sequel
% sudo gem install merb_sequel

Database Configuration

With the dependencies.rb file updated to specify which ORM you will be using you are ready to run Merb have have it create a sample database YAML file so you can configure merb for your specific database type and name.

% merb
Merb started with these options:
--- 
:cache_templates: false
:reloader_time: 0.5
:use_mutex: true
:host: 0.0.0.0
:session_id_cookie_only: true
:port: "4000"
:query_string_whitelist: []

:reloader: true
:environment: development
:session_secret_key: MY_MERB_PRJ18910
:merb_root: my_merb_project
:exception_details: true

Started merb_init.rb ...
Loading dependencies...
No database.yml file found in my_merb_project/config.
A sample file was created called database.sample.yml for you to copy and edit.

Create the Database for the Project

Next, you will need to edit the config/database.yml file to define how Merb should access your database. You can begin by copying the config/database.sample.yml and then editing it.

% cp config/database.sample.yml config/database.yml

Your database.yml file should look similar to:

---
# This is a sample database file for the ActiveRecord ORM
:development: &defaults
  :adapter: mysql
  :database: my_merb_project_development
  :username: root
  :password: 
  :host: localhost
  :socket: /tmp/mysqld.sock

:test:
  <<: *defaults
  :database: my_merb_project_test

:production:
  <<: *defaults
  :database: my_merb_project_production

Next, create your databases (again, these instructions assume you are using MySQL as a database). The creation commands should be similar to:

% cp config/database.sample.yml config/database.yml
% # Create merb_ar_test_development
% mysqladmin -u root create my_merb_project_development
% # Create merb_ar_test_production
% mysqladmin -u root create my_merb_project_production
% # Create merb_ar_test_test
% mysqladmin -u root create my_merb_project_test

Test Your Handiwork

Now, start the merb server

% merb
Merb started with these options:
--- 
:cache_templates: false
:reloader_time: 0.5
:use_mutex: true
:host: 0.0.0.0
:session_id_cookie_only: true
:port: "4000"
:query_string_whitelist: []

:reloader: true
:environment: development
...
Started merb_init.rb ...
Loading dependencies...
Connecting to database...
Loading Application...
Compiling routes..
Loaded DEVELOPMENT Environment...
Not Using Sessions

Open up your favorite browser and point it to http://localhost:4000/. You should see the default merb startup page.

Congratulations. You are now running Merb.