Rest Controller plugin

I’ve been using this in my recent projects fairly successfully, so I’m putting it out there for anyone else to use.

Rest Controller (unoriginal name, I know) provides basic defaults for your 7 RESTful actions (index, show, edit, new, create, update, destroy) for both HTML and XML. There are some basic customization capabilities, but if you need to customize more than a couple things, you shouldn’t be using this plugin.

Installation

If you haven’t included svn.superruby.com in your plugin sources:
  script/plugin discover
  # Yes to http://svn.superruby.com/svn/plugins/
Finally:
  script/plugin install rest_controller

From the README

Rest Controller provides basic default actions for a RESTful controller which can be overridden easily.

There is basic customization capabilities of the default responses. If you need more customization, you should just implement the method in question to bypass this plugin. If you find yourself overriding more than one or two of the default methods, then you probably should not be using this plugin to begin with.

Usage

1
2
3
4
    # Controller with default actions.
    class ProductsController < ApplicationController
      rest_controller :product
    end

Default Actions/Responses

These examples use @product / @products, but your actual variable name will depend on the model. “rest_controller :customer” will result in @customer / @customers, for example.

Index      - find(:all) on your model.
  HTML --> renders index.rhtml
  XML  --> renders @products.to_xml
  RNG  --> renders @products.to_rng (if simply_relaxed plugin is installed.)

Show       - find(params[:id])
  HTML --> renders show.rhtml
  XML  --> renders @product.to_xml if found / Status Code 404 if not.

Edit       - find(params[:id])
  HTML --> renders edit.rhtml

New        - Model.new
  HTML --> renders new.rhtml

Update     - update(params[:id], params[:product])
  HTML --> sets flash[:notice] appropriately
           redirects to show(id) if successful / renders edit.rhtml if not
  XML  --> Status Code 204 if successful / Status Code 400 if not

Create     - create(params[:product])
  HTML --> sets flash[:notice] appropriately
           redirects to show(id) if successful / renders new.rhtml if not
  XML  --> Status Code 201 if successful / Status Code 400 if not

Destroy    - destroy(params[:id])
  HTML --> sets flash[:notice] appropriately
           redirects to index
  XML  --> Status Code 204 / Status Code 404

Overriding Responses You can override the default responses by either providing a custom respond_to block, or provide a block for a specific action/mime-type.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
    # Overriding some responses.
    class ProductsController < ApplicationController
      rest_controller :product

      # Override a DELETE/HTML response.
      destroy_responds_to(:html) { render :text => "GONE!" }

      # ... could also be formatted this way ...
      destroy_responds_to :html do
        render :text => "GONE!"
      end

      # Override all default responses for an action with a custom
      # respond_to block.
      show_responds_to do |wants|
        wants.html
        wants.xml { render :xml => @product.to_xml(... some includes ...) }
        wants.graph { ... }
      end
    end

If you need to customize the logic for a particular action and cannot accomplish your desired result with before/meantime filters, you can simply implement the action in your class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    # Implement your own actions.
    class ProductsController < ApplicationController
      rest_controller :product

      # Overrides the 'update' action.
      def update
        # Do something special for updating.
      end

      # You can trigger the default responses, if they still
      # apply.
      def index
        # Some special code.
        index_responder
      end
    end

Responders expect an instance variable by the name of the model. (Product == @product, etc). index expects the plural name (@products).

2 Responses to “Rest Controller plugin”

  1. gabs Says:
    i like.. but I get Template missing errors on all the methods. Do I need to create them? what are the requirements?
  2. Tal Says:

    Is there a new version of this plugin? or an old version somewhere that i can see if it works with rails 2.1?

Leave a Reply