Module Camping::Controllers
In: lib/reststop.rb

Methods

REST   _error   no_method   not_found  

Public Class methods

Calling REST "<resource name>" creates a controller with the appropriate routes and maps your REST methods to standard Camping controller mehods. This is meant to be used in your Controllers module in place of R <routes>.

Your REST class should define the following methods:

  • create
  • read(id)
  • update(id)
  • destroy(id)
  • list

Routes will be automatically created based on the resource name fed to the REST method. Your class must have the same (but CamelCaps‘ed) name as the resource name. So if your resource name is ‘kittens’, your controller class must be Kittens.

For example:

  module Foobar::Controllers
    class Kittens < REST 'kittens'
      # POST /kittens
      def create
      end

      # GET /kittens/(\d+)
      def read(id)
      end

      # PUT /kittens/(\d+)
      def update(id)
      end

      # DELETE /kittens/(\d+)
      def destroy(id)
      end

      # GET /kittens
      def list
      end
    end
  end

Custom actions are also possible. For example, to implement a ‘meow’ action simply add a ‘meow’ method to the above controller:

  # POST/GET/PUT/DELETE /kittens/meow
  # POST/GET/PUT/DELETE /kittens/(\d+)/meow
  def meow(id)
  end

Note that a custom action will respond to all four HTTP methods (POST/GET/PUT/DELETE).

Optionally, you can specify a :prefix key that will prepend the given string to the routes. For example, the following will create all of the above routes, prefixed with "/pets" (i.e. POST ’/pets/kittens‘, GET ’/pets/kittens/(\d+)’, etc.):

  module Foobar::Controllers
    class Items < REST 'kittens', :prefix => '/pets'
      # ...
    end
  end

Format-based routing similar to that in ActiveResource is also implemented. For example, to get a list of kittens in XML format, place a GET call to /kittens.xml. See the documentation for the render() method for more info.

[Validate]