Just A Summary

Piers Cawley Practices Punditry

'acts_as_resource' progress

Posted by Piers Cawley Thu, 25 Jan 2007 15:30:00 GMT

I’m very nearly ready to release acts_as_resource, I just have to pull up and tidy code that’s currently in my working directory’s ApplicationController and we’re laughing. However, I thought you’d like to see what my nested controller looks like.

class ChildrenController < ApplicationController before_filter :fetch_resources def index end def show end def edit end def create @child = @children.build(params[:child]) if @child.save flash[:notice] = ‘Child was successfully created.’ redirect_to child_path(@child) end end def update if @child.update_attributes(params[:child]) redirect_to child_path(@child) else render :action => ‘edit’ end end def destroy @child.destroy redirect_to children_path end end

I’ve removed the XML responses to save vertical space, but they work pretty straightforwardly.

Note that fetch_resources is absolutely generic. It is unscaffolded, uses the resourceful conventions and extra information provided by each model’s acts_as_resource declations to work out what instance variables should be called and sets them appropriately.

With a little work to set up a generically named variable as well as the conventionally named ones (say, resource_chain) and a wee bit of simply_helpful pixie dust (polymorphic paths), it should be possible to write a ResourceController class that does all the work for you but allows you to alter behaviour without having to rewrite an entire action method. But that’s definitely for the next release of the plugin.

Comments

Leave a response

Comments



Just A Summary