Skip to content
Stephen Caudill edited this page Sep 7, 2010 · 10 revisions

Here's a simple example of what a Rails controller with a nested resource might look like before and after a conversion to use decent_exposure:

Before

class PeopleController < ApplicationController
  respond_to :html, :xml, :json
  before_filter :get_company

  def index
    @people = @company.people.where(params.except(:action, :controller, :format))
  end

  def show
    @person = @company.people.find(params[:id])
  end

  def new
    @person = @company.people.build
  end

  def edit
    @person = @company.people.find(params[:id])
  end

  def create
    @person = @company.people.build(params[:person])
    @person.save
    respond_with(@person)
  end

  def update
    @person = @company.people.find(params[:id])
    @person.update_attributes(params[:person])
    respond_with(@person)
  end

  def destroy
    @person = @company.people.find(params[:id])
    @person.destroy
    respond_with(@person)
  end

  private
  def get_company
    @company = Company.find(params[:company_id])
  end
end

After

class PeopleController < ApplicationController
  respond_to :html, :xml, :json

  expose(:company) { Company.find(params[:id]) }
  expose(:people) do
    company.people.where(params.except(:action, :controller, :format))
  end
  expose(:person)

  def create
    person.save
    respond_with(person)
  end

  def update
    person.update_attributes(params[:person])
    respond_with(person)
  end

  def destroy
    person.destroy
    respond_with(person)
  end
end