Announcing resources_controller
February 2nd, 2007
Update: check out resource s controller update
Update: check out this entry
Update: for Rails <= 1.2.2 users, also install this plugin
So you want to write a quick and dirty REST server:
- Models, ActiveRecord mmmm nice and DRY tick
- Routes, super easy - even nesting routes! tick
- Controllers - oh crap, we're talking scaffoling, then changing like 8 lines of code in each one of the nested controllers - there's major duplication - there must be a better way...
The above inspired me to come up with the resources_controller plugin (svn, and rdoc).
(If you're interested in how this works, and you don't want to read the rdoc, read my post at rails weenie)
Here's an example, your usual forums/posts/comments one. See the docs for more examples.
class ForumsController < ApplicationController
resources_controller_for :forums
end
class PostsController < ApplicationController
resources_controller_for :posts, :in => :forum
end
class CommentsController < ApplicationController
resources_controller_for :comments, :in => [:forum, :post]
end
Couple of caveats: you need rspec to run the tests, and there's no resource_controller (singleton resource controller) plugin as yet.
Please let me know how to make it better.
Update
2/Feb: Much better docs, and a few improvements
5 Responses to “Announcing resources_controller”
Sorry, comments are closed for this article.
Packagethief Says:
February 2nd, 2007 at 02:12 PM
This is very similar to something a cohort and I have been hacking on lately, though yours is far more complete (and better tested!). See Hampton's write-up on what we're cooking at: http://hamptoncatlin.com/2007/glimpse-of-letit-rest
I'm going to download this now and poke around. Nice work!
Ryan Daigle Says:
Ian, this is great. I've had to do something similar for my deeply nested model but haven't tied it all together as nicely as you have. I will definitely take a look at it and use it for some of my work. Much thanks.February 2nd, 2007 at 02:12 PM
Ian Says:
February 2nd, 2007 at 02:13 PM
Glad it's interesting for youse guys.
I'd be keen to get some input on making it look and reel as railsy as possible. Even if that means cutting stuff out. My motivation for this is: 6 months down the line it should still be useful - more pairs of eyes will help on this.
Robert Najlis Says:
the link for Rails <= 1.2.2 users http://svn.ardes.com/rails_plugins/resources_controller_lte_1_2_2 does not seem to be working...December 4th, 2007 at 09:28 PM
Ian White Says:
Hi Robert, If you are still on rails 1.2.2 then you can access this plugin at the deprecated area: http://svn.ardes.com/rails_plugins/deprecated/flatlining/resources_controller_lte_1_2_2/December 5th, 2007 at 01:24 PM