Archive for the ‘Merb’ Category

Merb is Getting Merged Into Rails 3

Dec 23rd, 2008 by Robert Dempsey - Tags: ,

Leave a Comment

Oh yeah, this should rock. Read the goodies on the Ruby on Rails blog.

There are a couple ways to get the latest and greatest Merb 0.9.0 (dev) installed and running. In this post I will go over the following points:

1. Installing git
2. Getting the latest merb-core
3. Getting the latest merb-more
4. Getting the latest merb-plugins
5. Keeping the the above merb components updated
6. Starting a merb app

Let’s get started!

1. Installing git

First, I suggest you install git using MacPorts. If you don’t have that installed, it’s pretty straight forward.

Once you have MacPorts installed, just run the following command:

sudo port install git-core

Go get something to drink or eat, it can take a bit of time. Be patient.

You can also download and install it with the typical ./configure, make, make install commands if you desire.

2. Get the latest merb-core

Note: If you have previous version of merb-core, I would suggest you uninstall it before installing this version of merb-core.

merb-core is officially hosted at github. We need to clone merb-core, basically the equivalent of svn checkout.

git clone git://github.com/wycats/merb-core.git

To install the gem from the checked out code:

cd merb-core
sudo rake gem install

merb-core 0.9.0 is now installed.

3. Getting the latest merb-more

What we will do here is basically the same thing we did for merb-core. Again, it is suggested that if you have a previous 0.9 release installed to remove it first.

git clone git://github.com/wycats/merb-more.git
cd merb-more
sudo rake gem install

4. Getting the latest merb-plugins

Installing the plugins is very similar with one caveat, you have to install the plugins you want each separately. I installed merb_datamapper, merb_helpers, and merb_rspec.

First checkout the merb-plugins:

git clone git://github.com/wycats/merb-plugins.git

Next, install each plugin you want, for example:

cd merb-plugins/merb_datamapper
sudo rake gem install
cd ../merb_helpers
sudo rake gem install
cd ../merb_rspec
sudo rake gem install

That’s it. The you should now have the latest merb 0.9.0 (dev) installed on your system ready for use.

If you are using DataMapper like myself, then you will also want the latest of that too. It’s really simple. DataMapper has both a git and svn repo, lets use the git repo for consistency here.

Also, if you don’t have the DataObjects MySQL driver installed you will need to get that first by doing the following:

sudo gem install do_mysql

Then you can install DataMapper from git with the following:

git clone git://github.com/sam/dm.git
cd dm
sudo rake gem install

Now you have all the tools you need installed to get starting cranking out some super merb apps. Don’t forget you can also pick your own Javascript Framework. I’m partial to jQuery, but you can choose any of the other great javascript libraries out there too. Mootools, YUI, extJS, or even Prototype (like Rails) just to name a few.

5. Keeping the the above merb components updated

It is real easy to keep the code you just checked out updated. For example to update your merb-core gem:

cd merb-core
git pull
sudo rake gem install

That’s it! We just updated our merb-core gem.

git pull is the equivalent of svn up. So, just run that on all your components you are using and then install them.

6. Starting a merb app

Merb is a little different then what you might be used to in rails. Lets create an example application and configure it to use DataMapper.

To create a new application, merb has a special script called merb-gen.

merb-gen myapp

That will create an app called myapp. Lets setup our database configuration next.

First, open your project in your favorite text editor (I’m partial to TextMate).

mate myapp/

Open up the init.rb file and go to line #19 and uncomment the line to use DataMapper:

### Uncomment for DataMapper ORM
use_orm :datamapper # We uncommented this line

Next, setup the database configuration for DataMapper:

cd myapp
merb-gen

That will create a sample database.sample.yml file for DataMapper. Rename it to database.yml and update the credentials and database name to suit your needs. It’s just like what you are used to in a Rails app, so it should look familiar. Here is an example of mine for MySQL:

---
# This is a sample database file for the DataMapper ORM
:development: &defaults
  :adapter: mysql
  :database: myapp_development
  :username: root
  :password:
  :host: localhost

:test:
  <<: *defaults
  :database: myapp_test

:production:
  <<: *defaults
  :database: myapp_production

Finally, now we can start working on our app. The generators are also in merb-gen. To see all the generators and there options run merb-gen with no arguments or merb-gen and the generator name with no arguments. For example, inside your application directory:

merb-gen

That will display all the generators available.

merb-gen resource

Will display the options for the resource generator.

Let's create a user resource:

merb-gen resource user

That's it. It will auto generate the controller, views, model, and test files for you.

If you get an error about merb_rspec, open up your init.rb file and add the following line at about line #31:

require "merb_rspec" # we added this line
use_test :rspec

This is just a temporary fix until it's fixed in the latest merb-core. According to this ticket: http://merb.lighthouseapp.com/projects/7433/tickets/108-merb-unable-to-get-merb_rspec#ticket-108-2 It should be fixed, but it's still not working for me.

That should be it! Start poking around, checking out the generators, running some demo apps. If you have any questions please post a comment below and I can try and help you figure it out.

Shameless Plug:
If you enjoyed this blog post, please recommend me on Working with Rails:
http://workingwithrails.com/person/9142-chris-kaukis

Here is a short tutorial about getting up and running with the latest Merb (currently 0.5.3) and DataMapper (currently 0.3.0).

Here is the summary of what we will need:

  • datamapper – A lightweight ORM
  • do_mysql – DataObjects MySQL driver used by DataMapper
  • merb – The up and coming Ruby Framework
  • merb_datamapper – Plugin for DataMapper
  • merb_helpers – A set of helper methods; mainly form helper methods
  • merb_has_flash – Unofficial plugin to add a flash session like Rails

There are two ways to install Merb, DataMapper, and it’s dependencies listed above:

  • SVN
  • GEMS

For this tutorial I am going to use SVN (except for merb_has_flash and the MySQL driver). So, lets get started!

1. Install DataMapper

svn co http://datamapper.rubyforge.org/svn/trunk/ data_mapper
cd data_mapper
sudo rake gem install

2. Install the DataObjects MySQL driver.

sudo gem install do_mysql

3. Install Merb

svn co http://svn.devjavu.com/merb/trunk merb
cd merb
sudo rake gem install

4. Install the Merb DataMapper plugin

svn co http://svn.devjavu.com/merb/plugins/merb_datamapper
cd merb_datamapper
sudo rake gem install

5. Install Merb Helpers plugin

svn co http://svn.devjavu.com/merb/plugins/merb_helpers
cd merb_helpers
sudo rake gem install

6. (Optional) If you want to have a flash session like in Rails, install the merb_has_flash plugin. Just so you know, this is not an official Merb plugin.

sudo gem install merb_has_flash

Now that Merb is installed we can start a Merb Application.

merb myapp

This will create a directory structure similar to Rails.

1. First thing we need to do is open up the config/dependencies.yml file. In here we will tell Merb to use DataMapper and about the Merb Helpers and Merb Flash plugin. It is pretty straight forward. Just uncomment a single line.

### Uncomment for DataMapper ORM
use_orm :datamapper

Next add the following below where it says “Add your other dependencies here”

### Add your other dependencies here
dependencies "merb_helpers", "merb_has_flash"

2. Next we should make sure our database is correctly set and created. Create a config/database.yml. It should look something like this:

---
# This is a sample database file for the DataMapper ORM
:development: &defaults
  :adapter: mysql
  :database: myapp_development
  :username: root
  :password:
  :host: localhost

:test:
  <<: *defaults
  :database: myapp_test

:production:
  <<: *defaults
  :database: myapp_production

Next create your database:

mysql -u root
create database myapp_development;
create database myapp_test;
exit

3. Enable sessions by opening up your config/merb.yml and tell Merb to use DataMapper for storing sessions.

# Uncomment if you have more than one ORM or if you need to be specific about
# which memory store to use. Built-in options are: memory, cookie, or mem_cache
#:session_store: sequel
:session_store: datamapper # We added this line

Now create the sessions table.

rake dm:sessions:create

That's it! You have now installed the latest and greatest Merb and DataMapper with all it's dependencies. To update any of the components you downloaded from SVN just go to the directory and type the following:

sudo rake gem install

To run the local development server, just type the following inside your applications directory:

merb

To run the Merb interactive console, just type the following within your applications directory:

merb -i

That should get you at least started and poking around with the Merb framework and DataMapper.

Next, we will go over common Railisms and the Merb equivalents. Look for that in the next day or two.

Links:

http://merbivore.com/

http://datamapper.org/

http://svn.devjavu.com/merb/trunk

http://svn.devjavu.com/merb/plugins/merb_helpers

http://svn.devjavu.com/merb/plugins/merb_datamapper

http://datamapper.rubyforge.org/svn/trunk/

http://merb.devjavu.com/browser/plugins/merb_datamapper

If you liked this post or any of my other previous posts, please recommend me on Working with Rails.

Collaborate.
Enable.
Succeed.

Contact

(888) 331-8520
4210 Beau James Court
Winter Park, Florida 32792 RSS Feed

Search

Popular Articles

Recent Articles