#92
Feb 11

make_resourceful

The make_resourceful plugin is a great way to DRY up the 7 RESTful actions common in most controllers. Learn how to use it in this episode.
Tags: plugins
Download (15 MB, 6:08)
alternative download for iPod & Apple TV (9 MB, 6:08)
Resources
script/plugin install http://svn.hamptoncatlin.com/make_resourceful/tags/make_resourceful
# products_controller.rb
make_resourceful do
  actions :all

  response_for :show do |format|
    format.html
    format.xml { render :xml => current_object.to_xml }
  end
end

private

def current_objects
  Product.find(:all, :order => 'name')
end

def current_object
  @current_object ||= Product.find_by_permalink(params[:id])
end
<!-- edit.html.erb -->
<%= hidden_field_tag '_flash[notice]', "Successfully updated product." %>

16 comments:

REST? Feb 11, 2008 at 00:54

What do you think about resource_controller http://www.akitaonrails.com/2008/1/25/easy-restful-rails-screencast


Paukul Feb 11, 2008 at 01:11

thanks for this episode. It's a great way to cover the RESTful controllers for standard Model access in a nice and dry way.


sintaxi Feb 11, 2008 at 01:32

First of all I think this is a beautiful plugin. I feel though that it is perhaps clever to a fault. Thats adding 572k to my project so I *cannot see my controllers? Even if I were to stick completely to the REST convention I would rather run 'scaffold' and move on. That being said I am happy to see people taking REST this seriously.


Matt Hooks Feb 11, 2008 at 01:43

Any tips on using this with RESTful controllers that do not control their own model (i.e. a sessions controller for User authentication)? Or would you recommend not using m_r in that case?


August Lilleaas Feb 11, 2008 at 02:13

@Matt Hooks: The whole point of make_resourceful is to dry up the default model CRUDS, so if you're doing anything other than that, I'd say typing the SessionsController out by hand would make sense.


cesidio Feb 11, 2008 at 04:46

@akita
I follow your blog and watched the last video u made about resource_controller and rails 2.0.
It was very good, but I think it would have been better if u used:

make_resourceful vs resource_controller
haml vs erb

these 2 plugins simplify the development of web apps, please take a look at them and make another great video!!

http://haml.hamptoncatlin.com/
http://mr.hamptoncatlin.com/

BTW
thanks ryan for this great screencast, u r still the leader of screencasts!!!


REST? Feb 11, 2008 at 08:35

I am not Akia.


Ryan Bates Feb 11, 2008 at 08:39

@REST, I wasn't very familiar with resource_controller, but taking another look at it now it looks very good and in some ways looks more powerful and concise than resource_controller. I should have mentioned it in the screencast as an alternative, thanks for pointing it out.

@sintaxi, in some ways I agree with you. Trying to DRY up the controller can often lead to a solution that's too clever for its own good. But at the same time I think it can lead to more concise and readable code as long as you use it properly and understand the conventions. Instead of thinking about it hiding the controller actions, think of it as highlighting the exceptions to the conventions, which is often more important.


Jason Lee Feb 11, 2008 at 10:01

Or there's the original resources_controller

http://agilewebdevelopment.com/plugins/resources_controller


sambo99 Feb 12, 2008 at 14:01

@Ryan,

I think Hampton rules, HAML is gold and from what I am seeing make_resourceful looks really nice. For my current project I'm using resource_controller, I really like the new scaffolding which is HAML based if you have HAML installed, I also like the fact my controllers are completely empty of code. Farther more RC is really hackable, I managed to write a new base class that gives me base level handling for my xhr calls, (which is not handled by default)

I don't think its a case of one plugin is better than the other, as hampton says: "two different approaches, with different strengths"

As usual, your screencasts are great.

PS. can you make this text area a bit bigger, its hard to type in long comments :)


AkitaOnRails Feb 13, 2008 at 05:47

Actually it wasn't me that posted the first message :-) Anyway. make_resourceful is a tremendous plugin and it came first. Caitlin definitely has the edge. James Golick used to contribute to this plugin but decided to make his own because of his disagreements with some of the concepts of the original project. Alas, the 'big' block around everything for one.

Now, to me, the biggest benefit of resource_controller over make_resourceful are the view helpers that makes your view set virtually independent of nesting depth. If you're working with polymorphic associations, this is a must have.


RyanBatesRules Feb 13, 2008 at 13:48

Ryan, thanks for yet another insightful cast. They are even great for brushing up on TextMate tricks. I guess you're typing ^L when typing a hash pair for clarity, however I thought it wouldn't hurt to mention that this can even more easily be achieved by typing a colon followed by pressing 'tab'.


Doug Feb 17, 2008 at 14:54

I went through akita's screen cast and realized how powerful resource_controller was but had the sick feeling of how heavy the plugin was, excluding the app that comes bundled! I just found this screen cast and converted a project from r_c to m_r. I am happy with it as a much smaller plugin that hides most of the same work. The only thing I can't figure out is how to override the actual behavior of a rest method for associating objects like comments to posts. Response_for is only concerned with rendering. Where is the hook for the behavior?


Jocke Mar 14, 2008 at 10:56

I'm a bit curious about testing. If you where to use behavior driven development, you would want to describe the behavior of your controllers.

Now I know you should not test the make_resourceful functionality because that is bound to be well tested already.

I guess I can write tests for behavior that differs from the vanilla REST setup, but then I'm just assuming that I use make_resourceful in the controller, not testing that behavior...

In the end I guess it's just about rubust code, if it does not break when you make changes and update the tests... then all is fine.

Are there someone else that have given this some thought?


kino May 04, 2008 at 03:25

Of course, no controller only uses the defaults. So make_resourceful can be massively customized, while still keeping your controllers trim and readable.


HG Jul 13, 2008 at 07:58

This is a good plugin to know about.

As the video mentions you should use the plugin only if you have a conventional controller. I rarely have conventional controllers and I'd like to see a video on drying up those.

It wouldn't be a short video as there are many cases to consider, but I expect there are patterns that could be shown. Maybe there should be a book on the subject.

Add your comment:

(required)

(not displayed)

(SKIP THIS ONE)


(required)

subscribe:
sponsored by:
if you want to help:
required:
Get Quicktime Player