- Install Merb and it's dependencies
To install Merb, all you need to run is
sudo gem install merb --include-dependencies
- Generate your Merb application
To generate a your Merb app's folder (in this case, we'll call our app "merbtest"), all you need to run is
merb-gen app merbtest
This will generate all of the base files and folders for your Merb app. - Fire your app up
Technically, at this point you can fire your app by just running
merb
from your app's root. By default, you'll be able to access your app at http://localhost:4000. You will get the default "Welcome to Merb" page which also tells us, amongst other things, how to get rid of this page (This page also happens to be the 404 page. You can view it in the app/views/exceptions folder). - Setup your routes
Your route configuration will take place in the config/router.rb file. Like Rails, you get the default route of ":controller/:action/:id" which is fine unless you have a number of "resource" routes and you want to prevent GET requests from hitting your create and destroy methods (or any other method you want to be only accessible via POST). For the purpose of this introduction to Merb, let's set a specific route for our controller we have yet to create. Comment out the r.default_routes and add this route:
r.match('/hello').to(:controller => 'hello', :action => 'show')
. This will map any requests sent to localhost:4000/hello to the hello controller's show method. I guess we should go create that controller. - Creating Controllers
To create the "Hello" controller, run
merb-gen controller hello --testing-framework rspec
Telling the generator what testing framework you're using lets merb generate the related test files which in our case would be a spec file for our controller and helper we just created. Run merb-gen controller
if you'd like to see the other optional flags. Now that we have a controller, all we need to do is define our show method and create the associated view template. Add this to your Hello controller
def show
@hello = 'hi'
render
end
and this to your app/views/hello/show.html.erb file: say <%= @hello >
and that's it.
- Setting up your ORM
As far as ORM's go, I've been wanting to try Sequel, so let's run with that. to install it, run:
sudo gem install merb_sequel
and in your config/init.rb add or uncomment
use_orm :sequel
Now we have Sequel ready and willing. Run merb in your app root to have Sequel create a sample database.yml in your config folder you can copy from. Your database.yml file will need to look something like this:
:development: &defaults
:adapter: mysql
:database: merbtest_development
:username: root
:password:
:host: localhost
:test:
<<: *defaults :database: merbtest_test
:production:
<<: *defaults :database: merbtest_production
To actually populate the database we'll need a model and an actual migration to set our table up. Let's start with the model. Run
merb-gen model book
And now we have our model and model spec file. To generate our first migration just run
merb-gen migration add_books_table --orm sequel
Or, at least it should create the migration for you. When I wrote this, I wasn't getting any migration files created. If you don't get any migration files, create it! Either way, your schema/migrations/001_add_books_table.rb file should have the following:
class AddBooksTable < Sequel::Migration
def up
create_table :books do
primary_key :id
varchar :title, :author
timestamp :created_at
text :description
end
end
def down
execute 'DROP TABLE books'
end
end
To actually run the migration, all you need to do is run
rake sequel:db:migrate
I'll leave the actual populating of the database with actual data as an exercise for the reader :)