Monday, March 9, 2009

Merb It Is, Then

‹prev | My Chain | next›

It would seem that Rail3 is not under much active development at this point and that most of the effort to date made it into Rails 2.3. Here's hoping activity picks up again soon.

In the meantime, I would like to gain some experience with Merb (sadly, I have no direct experience), so I'll do a quick spike of running Merb and CouchDB. Happily addwaddy has a quick intro on running Merb+CouchDB that I'll be following.

The first thing that I need to do is install Merb:
cstrom@jaynestown:~$ gem install merb
WARNING: Installing to ~/.gem since /usr/lib/ruby/gems/1.8 and
/usr/bin aren't both writable.
Building native extensions. This could take a while...
Building native extensions. This could take a while...
Successfully installed addressable-2.0.2
Successfully installed extlib-0.9.10
Successfully installed data_objects-0.9.11
Successfully installed dm-core-0.9.10
Successfully installed dm-migrations-0.9.10
43 gems installed
Whoa, that was a lot. Good thing I have --no-ri --no-rdoc in my .gemrc file.

Next up is generating the Merb application skeleton:
cstrom@jaynestown:~/repos$ merb-gen app eee-merb
Generating with app generator:
[ADDED] .gitignore
[ADDED] public/.htaccess
[ADDED] tasks/doc.thor
[ADDED] public/javascripts/jquery.js
[ADDED] doc/rdoc/generators/merb_generator.rb
I add the following attributes to the config/database.yml file:
development: &defaults
adapter: couchdb
database: eee-meals
port: 5984
Feeling plucky, I also add an entry for the test DB:
<<: *defaults
database: eee-test
Next up, we generate a meal resource:
cstrom@jaynestown:~/repos/eee-merb$ merb-gen resource meal
Loading init file from /home/cstrom/repos/eee-merb/config/init.rb
Loading /home/cstrom/repos/eee-merb/config/environments/development.rb
Generating with resource generator:
Loading /home/cstrom/repos/eee-merb/config/environments/development.rb
Loading /home/cstrom/repos/eee-merb/config/environments/development.rb
Loading /home/cstrom/repos/eee-merb/config/environments/development.rb
[ADDED] spec/models/meal_spec.rb
[ADDED] app/models/meal.rb
[ADDED] spec/requests/meals_spec.rb
[ADDED] app/controllers/meals.rb
[ADDED] app/views/meals/index.html.erb
[ADDED] app/views/meals/show.html.erb
[ADDED] app/views/meals/edit.html.erb
[ADDED] app/views/meals/new.html.erb
[ADDED] app/helpers/meals_helper.rb
resources :meals route added to config/router.rb
Then I edit the meals.rb file:
class Meal
include DataMapper::Resource

property :id, String, :serial => true, :key => true, :field => :_id
property :rev, String, :field => :_rev
property :title, String
property :date, String
property :summary, String
property :description, String
At this point, I'm ready to give the application a whirl, but end up with this failure:
cstrom@jaynestown:~/repos/eee-merb$ merb
Loading init file from /home/cstrom/repos/eee-merb/config/init.rb
Loading /home/cstrom/repos/eee-merb/config/environments/development.rb
~ Connecting to database...
/usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require': no such file to load -- /home/cstrom/.gem/ruby/1.8/gems/dm-core-0.9.10/lib/dm-core/adapters/couchdb_adapter (LoadError)
from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
from /home/cstrom/.gem/ruby/1.8/gems/dm-core-0.9.10/lib/dm-core.rb:146:in `setup'
from /home/cstrom/.gem/ruby/1.8/gems/merb_datamapper-1.0.9/lib/merb/orms/data_mapper/connection.rb:44:in `setup_connections'
from /home/cstrom/.gem/ruby/1.8/gems/merb_datamapper-1.0.9/lib/merb/orms/data_mapper/connection.rb:27:in `connect'
from /home/cstrom/.gem/ruby/1.8/gems/merb_datamapper-1.0.9/lib/merb_datamapper.rb:32:in `run'
from /home/cstrom/.gem/ruby/1.8/gems/merb-core-1.0.9/lib/merb-core/bootloader.rb:99:in `run'
from /home/cstrom/.gem/ruby/1.8/gems/merb-core-1.0.9/lib/merb-core/server.rb:172:in `bootup'
from /home/cstrom/.gem/ruby/1.8/gems/merb-core-1.0.9/lib/merb-core/server.rb:42:in `start'
from /home/cstrom/.gem/ruby/1.8/gems/merb-core-1.0.9/lib/merb-core.rb:170:in `start'
from /home/cstrom/.gem/ruby/1.8/gems/merb-core-1.0.9/bin/merb:11
from /home/cstrom/.gem/ruby/1.8/bin/merb:19:in `load'
from /home/cstrom/.gem/ruby/1.8/bin/merb:19
Ah, no worries, the DataMapper CouchDB adapter needs to be installed (the addywaddy site indicated that it was part of dm-core). It is a simple install:
cstrom@jaynestown:~/repos/eee-merb$ gem install dm-couchdb-adapter
WARNING: Installing to ~/.gem since /usr/lib/ruby/gems/1.8 and
/usr/bin aren't both writable.
Successfully installed dm-couchdb-adapter-0.9.10
1 gem installed
Unfortunately, that will have to do tonight. I was unable to get the data to get pulled back from CouchDB. Merb is pulling the data from CouchDB (according to the couch.log file), but Merb still returns a 404 for some reason...

Update: I did try mixing in DataMapper::CouchResource, instead of the default DataMapper::Resource, into the Meal class, but still got similar results. The CouchDB log shows a 200 response, but I still end up with a 404 in Merb. I converted the Meal.get to a Meal.get! in the controller's show method and get:
merb : worker (port 4000) ~ Could not find Meal with key ["123"] - (DataMapper::ObjectNotFoundError)
/home/cstrom/.gem/ruby/1.8/gems/dm-core-0.9.10/lib/dm-core/model.rb:248:in `get!'
/home/cstrom/repos/eee-merb/app/controllers/meals.rb:10:in `show'
I'm not familiar enough with Merb to know where to go from here. But, something to do tomorrow.

Update #2: Figured it out. I was trying to pull back data created outside of Merb. The Couch DataMapper adapter requires a couchdb_type attribute to be set. That explains why the CouchDB logs reported the document as found, but DataMapper reported it as not found—the record returned from CouchDB was not a Meal data object in its eyes.

No comments:

Post a Comment