Saturday, April 14, 2007

How to Make Your ActionController Go Up In A Bang

Go on, try it. I dare you. I double-dare you:


class UserController < ActionController::Base  
def send
end

def request
end

def render
end
end

These are all natural enough names for controller methods, but your controller will mysteriously vanish, cease to operate in strange, strange ways if you use any of them.


Why? Because you are overriding methods crucial to the internals of the controller. In the first method, Ruby's Object#send. In the next two, ActionController methods.


How to save yourself time: I really hate silent failure or mysterious failure. But you can make the silent failures clear and noisy (oxymoronic, I know!). Try something like this:


module ActionController
class Base
def Base.method_added(sub)
raise "Cannot override action 'send'" if sub == :send
raise "Cannot override action 'request'" if sub == :request
raise "Cannot override action 'response'" if sub == :response
raise "Cannot override action 'render'" if sub == :render
#...
end
end
end

No comments: