Table of Contents

FAQ: How can I convert my Rails app to a Merb app?

So you're heard that Merb is the hot-diggity and you're ready to convert your latest greatest Rails creation to Merb. Below are some basics to get you started.

THIS IS VERY MUCH A WORK IN PROGRESS. Please edit to make it better.

First, should you?

So you're familiar with Rails. It's working great for your app. Things are running well and plenty fast. Do you have a real reason to convert? Many Rails apps are just fine (and will live and die quite happily) being Rails apps their entire life-cycle. It's not an either/or world out there. You may want to use Rails for one project and Merb for another. If you're happy with Rails… it may be time to stop reading.

It could be you've hit the spot where Rails isn't shining as much as it used to…

ORM Agnostic == Long Live ActiveRecord

You've heard Merb is ORM agnostic. This is good news for the Rails conversion. Below are steps to convert your models to Merb.

Yep, that's it… you're done. You can now fire up merb and hit up your models…

# merb -i

>> Forum.find(:first)
=> #<Forum ...>
>>

Controllers

Typical Changes

Examples

Here is a typical update action in Rails.

  def update
    @forum = Forum.find(params[:id])

    respond_to do |format|
      if @forum.update_attributes(params[:forum])
        flash[:notice] = 'Forum was successfully updated.'
        format.html { redirect_to(@forum) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @forum.errors, :status => :unprocessable_entity }
      end
    end
  end

Here is the same action in Merb (with AR).

  def update(id, forum)
    provides :html, :xml
    @forum = Forum.find(id)
    if @forum.update_attributes(forum)
       return render(:nothing => 200) if content_type==:xml
       redirect resource(@forum), :message => {:notice => 'Forum was successfully updated.'}
    else
      return render(:xml => ?) if content_type==:xml
      display @forum, :edit
    end
  end

Plugins

Authentication

Deployment

Development

someapp/config/environments/development.rb

rails:

config.after_initialize do
  ActiveMerchant::Billing::Base.mode = :test
end

merb:

Merb::BootLoader::after_app_loads do
  ActiveMerchant::Billing::Base.mode = :test
end

Glossary

Rails Merb
script/server merb
script/console merb -i
script/generate merb-gen
redirect_to article_path(@article) redirect resource(@article)
respond_to provides :xml, :js, :yaml (respond to is automatic in Merb but a block can be used)
format content_type
render :xml ⇒ @article render @article
render :file ⇒ 'public/404.html, :status ⇒ 404 raise NotFound
logger Merb.logger (e.g. Merb.logger.info(“Setting coordinates”))
before_filter before
render :partial partial
Rails.env Merb.environment
flash[:notice] message[:notice]
rake routes rake audit:routes
rake db:migrate rake db:automigrate