The combination of a running nose, scratchy throat, and this ActiveResource bug made for a lovely afternoon.
The form_for method in ActionView::Helpers::FormHelper, when given an ActiveRecord object as an argument, uses the new_record? method to determine whether the form action should be POST or PUT. Well, today I was building a controller to consume a restful web service, so I was using ActiveResource instead of ActiveRecord.
Everything was hunky-dory until I began doing a little testing.
Whenever I would submit my 'new' form, it would blow up on a before_filter method. The odd part was that the before_filter method was declared as such...
before_filter :do_stuff, :except => [:new, :create]
WTF? It should not have been calling the do_stuff method before the 'create' action. Well, as it turns out, it wasn't. A little debugging helped me realize that it was actually hitting the 'update' action. Again, WTF?
I checked the form_for method call. I checked the generated html. I checked the routes file. Everything looked kosher. I was stumped.
When all else fails, google that shit.
I did, and what did I find? This Lighthouse ticket describing how form_for no worky with ActiveResource in Rails 2.2.1 (and 2.2.2 evidently since that what I was running). So what's the solution?
alias :new_record? :new?
Add that to your resource model. ActiveResource defines a new? method which works exactly like ActiveRecord's new_record? method.
I had actually checked the new_record? method when I was debugging the issue, but for some reason no bells and whistles went off when @thing.new_record? returned nil. WTF Brent? I blame it on the medication.

