<?xml version="1.0" encoding="UTF-8"?>
<post>
  <content>Here's a collection of notes made while I was working on migrating this blog from "Rails":http://rubyonrails.com/ to "Merb":http://merbivore.com/ (no, the Merb version isn't live yet). These are relevant to version 0.5.2 - but Merb's moving so fast these days, I wouldn't be surprised if much of this isn't relevant any more.

Most of this you can find if you look through the documentation, but if you've not yet played with Merb, this will hopefully give you some idea of some of the smaller differences.

h3. Filters

* Use @before@ and @after@ instead of @before_filter@ and @after_filter@
* Instead of @:except@, use @:exclude@.
* To kill the chain of filters and not follow on to the action method, you need to use the @throw@ method:

&lt;pre&gt;&lt;code&gt;def confirm_user_session
  throw :halt, Proc.new { |controller|
    controller.redirect url(:new_session)
  } if current_user.nil?
end&lt;/code&gt;&lt;/pre&gt;

Oh, and in case you missed it in the above sample, @redirect_to@ becomes @redirect@.

h3. Routes

Resource routing isn't actually any different to Rails - but I had trouble finding any documentation for it, so it wasn't obvious to start with. This code will work in both Merb and Rails (although in Rails @r@ is usually referenced as @map@)

&lt;pre&gt;&lt;code&gt;r.resources :posts do |post|
  post.resources :comments
end&lt;/code&gt;&lt;/pre&gt;

However, using these routes is definitely different. The @url_for@, @route_url@ and @route_path@ methods aren't around - you need to use Merb's magical @url@ method:

&lt;pre&gt;&lt;code&gt;url(:post, @post@)
url(:new_session)&lt;/code&gt;&lt;/pre&gt;

h3. View Helpers

Helpers are pretty minimal in Merb - you don't get any of the inflectors, or the number formatters. And no form helpers - not in the core gem, anyway. You can get some of those from the @merb_helpers@ gem, but they don't match Rails' syntax and method naming, so it can take a bit of time to get this switched over, depending on the size of your app.

No support for Form Builders in that gem, by the way.

Some of the default helpers that do exist have different names to their Rails counterparts. A few of the ones I came across:

* @content_tag@ =&gt; @tag@
* @tag@ =&gt; @open_tag@
* @content_for@ =&gt; @throw_content@
* @javascript_include_tag@ =&gt; @js_include_tag@
* @stylesheet_include_tag@ =&gt; @css_include_tag@

h3. All your favourite plugins

Because Merb plugins are gems, anything you use as a plugin in Rails is pretty unlikely to be ported over. "will_paginate":http://errtheblog.com/posts/56-im-paginating-again was the main one for me, so I ended up pulling the files into my lib directory. Of course, that was only really useful when using ActiveRecord - DataMapper and Sequel users may have to get hacking into any ActiveRecord-focused plugins they want to use.

h3. Partials

No more @render :partial@ - you want something more like the following:

&lt;pre&gt;&lt;code&gt;partial "comments/show", :with =&gt; @post.comments.active
partial "post", :with =&gt; @posts, :as =&gt; :post&lt;/code&gt;&lt;/pre&gt;

Again - you'll find most/all of this in the documentation, but the only potential show-stopper I found was plugins - the rest isn't that that big a difference to Rails.</content>
  <created-at type="datetime">2008-03-09T10:52:36+00:00</created-at>
  <created-by type="integer">1</created-by>
  <deleted-at type="datetime" nil="true"></deleted-at>
  <deleted-by type="integer" nil="true"></deleted-by>
  <id type="integer">36</id>
  <internal type="boolean">false</internal>
  <path>migrating_code_from_rails_to_merb</path>
  <subject>Migrating Code from Rails to Merb</subject>
  <updated-at type="datetime">2008-03-09T10:53:48+00:00</updated-at>
  <updated-by type="integer" nil="true"></updated-by>
</post>
