Rails will use rescue_action_in_public for errors in production,
unless the the request comes from the same IP as the server. Then,
you get the standard dev-mode stack trace screen.
What's the prefered way to react when such an error occurs?
Briefly, @user might be nil, so you must guard it with "if @user" each time you use it.
I prefer to put that "if @user" line in only one place, for DRY code. I use a kind of Null Object there:
def someone
return @user if @user
who = OpenStruct.new(
:login => '',
:namen => '',
:user_id => nil)
def who.delete_data(r)
false
end
return who
end
Instead of using "@user" everywhere, I use "someone". If nobody is logged in, "someone" provides only methods that un-logged-in people can use. For example, if logged-in users can delete data, the line "someone.delete_data(a_record)" will delete the data if a user is logged in, and it won't do anything if nobody is logged in.
The heart of Object Oriented programming is replacing if statements with virtual methods that provide the correct behavior.
Tip: Don't use rescue where you should just use if. The @user is just nil, per the contract of .find(), so use if @user to detect that. Don't let the nil run around loose until something explodes, and then rescue that!