I wanted to be able to clone fields from one record into a new record,
so I added a select box to allow the user to select the record from
which (s)he wanted to clone (using AJAX to dynamically update the view
when (s)he selects such a record). In my #new action, I check to see
if the :clone_from parameter points to a valid record and if it does,
I set my @record parameter to that record (created via
old_record.clone). If it doesn’t point to a valid record, I set
@record to Record.new.
Here’s my problem…
in my view, I have
form_for record do |f|
…
end
which generates a RESTful call to my #create action when @record was
empty (i.e. filled in from Record.new), but generates a RESTful call
to my #update action when @record is a clone of the old record.
Ahhhh… this is why this mailing list is so helpful… I start to
write the email, I get to the part where I write “is a clone of the
old record”, and I think to myself, “Wait a minute! Did I really call
#clone when I went down that path?”… checked the code… found out
that I didn’t… called #clone… problem solved.
Gee, thanks folks!!! You’re the greatest!!! (especially those of you
who kept reading this far :-))
OK, now I’ve updated the subject line to reflect my (new) real
question…
When I first discovered this behavior, I thought to myself, “I should
write a test case that demonstrates this bug, fix the bug, and show
that the test case now passes”.
When I tried to write the functional test that demonstrated this
(issue a #get call to the :new action with the :clone_from parameter
set, issue a #post :create call with the modified clone), the test
worked perfectly. I had to look at the logs or HTML source to see
that I was calling #update instead of #create.
How could I have tested this? Is it even worth writing a test case to
demonstrate the incorrect operation of code that simply had a typo in
it?
What do you think?
–wpd