A word of Caution on method names

0 views
Skip to first unread message

Endikos

unread,
Apr 2, 2010, 4:17:55 PM4/2/10
to substruct
Hello all,

I've been banging my head on this, and now that I've figured it out,
thought I'd share my misfortune for your benefit :-)

My company decided we wanted to automatically send out an invitation
for customers to provide feedback regarding their orders 10 days after
an order was marked shipped. After digging into various ways to fire
a method to send out invitations daily (javan-whenever ftw), I started
building the necessary models and controllers to handle the survey
responses. I created a FeedbackController with two methods, index and
response, to handle the customer's response to our request for
feedback. After getting things coded and starting to test, I noticed
that every time I tried to redirect_to from anywhere in the
FeedbackController, I'd get a 500 Server Error. Everything else in
substruct still worked fine, inlcuding redirects in other
controllers. I stripped the offending controller down to nothing but
a redirect to try to figure out what was going on. Here's what it
looked like in its most minimal form:

class FeedbackController < ApplicationController
def index
redirect_to :controller => "store", :action => "index"
end

def response
end

end

And it I tried to hit http://localhost:3000/feedback/ I'd get a weird
stack trace that started like this:

Processing FeedbackController#index (for 127.0.0.1 at 2010-04-02
14:53:49) [GET]
Session ID: 5b7405a1bda67f6feea6858e560f005c
Parameters: {"action"=>"index", "controller"=>"feedback"}
Preference Load (0.000330) SELECT * FROM `preferences` WHERE
(`preferences`.`name` = 'store_name') LIMIT 1
Tag Load (0.001005) SELECT * FROM `tags` WHERE (parent_id IS NULL
OR parent_id = 0) ORDER BY -rank DESC
OrderUser Columns (0.001506) SHOW FIELDS FROM `order_users`
OrderUser Load (0.000396) SELECT * FROM `order_users` WHERE
(`order_users`.`id` IS NULL) LIMIT 1
Redirected to http://localhost:3000/store


NoMethodError (You have a nil object when you didn't expect it!
The error occurred while evaluating nil.redirect):
/vendor/rails/actionpack/lib/action_controller/base.rb:1054:in
`redirect_to'
/vendor/rails/actionpack/lib/action_controller/base.rb:1059:in
`redirect_to'
... SNIP ...

When looking at action_controller/base.rb the offended code was:

response.redirect(options, interpret_status(status))

Eventually it dawned on me that the cause of the whole mess (and
several days of frustration with an astounding amount of googling and
debug statements followed and stepped into to the nth degree) was that
I had a method named "response", and the Action controller was trying
to act on "response" to do the redirect I wanted. I renamed the
method to "customer_response" and everything's lovely again.

So, have a good chuckle at my expense and remember not to name any of
your method's "response", or you may be in for a serious bout of head-
scratching. :-)

Bill

Reply all
Reply to author
Forward
0 new messages