<?xml version="1.0" encoding="UTF-8"?>
<post>
  <body>&lt;p&gt;The combination of a running nose, scratchy throat, and this &lt;a href="http://api.rubyonrails.org/classes/ActiveResource/Base.html"&gt;ActiveResource&lt;/a&gt; bug made for a lovely afternoon.&lt;/p&gt;
&lt;p&gt;The &lt;a href="http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html#M001572"&gt;form_for&lt;/a&gt; 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.&amp;nbsp; Well, today I was building a controller to consume a restful web service, so I was using ActiveResource instead of ActiveRecord.&lt;/p&gt;
&lt;p&gt;Everything was hunky-dory until I began doing a little testing.&lt;/p&gt;
&lt;p&gt;Whenever I would submit my 'new' form, it would blow up on a before_filter method.&amp;nbsp; The odd part was that the before_filter method was declared as such...&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;before_filter :do_stuff, :except =&amp;gt; [:new, :create]&lt;/pre&gt;
&lt;p&gt;WTF? It should not have been calling the do_stuff method before the 'create' action.&amp;nbsp; Well, as it turns out, it wasn't.&amp;nbsp; A little debugging helped me realize that it was actually hitting the 'update' action.&amp;nbsp; Again, WTF?&lt;/p&gt;
&lt;p&gt;I checked the form_for method call.&amp;nbsp; I checked the generated html.&amp;nbsp; I checked the routes file.&amp;nbsp; Everything looked kosher.&amp;nbsp; I was stumped.&lt;/p&gt;
&lt;p&gt;When all else fails, google that shit.&lt;/p&gt;
&lt;p&gt;I did, and what did I find?&amp;nbsp; &lt;a title="Ruby on Rails - Lighthouse Ticket #1455" href="http://rails.lighthouseapp.com/projects/8994/tickets/1455-activeresource-failed-with-rails-221"&gt;This Lighthouse ticket&lt;/a&gt; describing how form_for no worky with ActiveResource in Rails 2.2.1 (and 2.2.2 evidently since that what I was running).&amp;nbsp; So what's the solution?&lt;/p&gt;
&lt;pre name="code" class="ruby"&gt;alias :new_record? :new?&lt;/pre&gt;
&lt;p&gt;Add that to your resource model.&amp;nbsp; ActiveResource defines a new? method which works exactly like ActiveRecord's new_record? method.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;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.&amp;nbsp; WTF Brent?&amp;nbsp; I blame it on the medication.&lt;/p&gt;</body>
  <created-at type="datetime">2009-03-17T02:04:42Z</created-at>
  <description>The combination of a running nose, scratchy throat, and this ActiveResource [http://api.rubyonrails.org/classes/ActiveResource/Base.html] bug made for </description>
  <id type="integer">25</id>
  <keywords></keywords>
  <published type="boolean">true</published>
  <slug>form-for-funkiness-with-activeresource</slug>
  <title>form_for funkiness with ActiveResource</title>
  <updated-at type="datetime">2009-05-04T04:06:34Z</updated-at>
</post>
