Search:

Search all manuals
Search this manual
Manual
Couchbase Client Library Ruby 1.1
Community Wiki and Resources
Download Client Library
RDoc
Ruby Client Library
SDK Forum
Additional Resources
Community Wiki
Community Forums
Couchbase SDKs
Parent Section
2.3 Not your mother's ActiveRecord
Chapter Sections
Chapters

2.3.2. Defining an API

Just because we aren't using ActiveRecord doesn't mean we can't have a nice API, thanks to ActiveModel we can very easily create a simple Model to encapsulate our Link object. What would such an API look like? Let's define how we would interact with a Couchbase model from our Controller.

Listing 6: app/controllers/links_controller.rb

class LinksController < ApplicationController
  def create
    @link = Link.new(params[:link])
    @link.session_id = session[:session_id]
    if @link.save
      respond_to do |format|
        format.html { redirect_to @link }
        format.js
      end
    else
      respond_to do |format|
        format.html { render :new }
        format.js
      end
    end
  end

  def short
    @link = Link.find(params[:id])
    redirect_to root_path unless @link
    @link.views += 1
    @link.save
    redirect_to @link.url
  end

  def show
    @link = Link.find(params[:id])
  end

  def new
    @link = Link.new
  end
end

So, in looking at our sketched out Controller above we can see how we would like to interact with our Link object.