Rails quality control tip: use verify in controllers

Tags
Tagsruby, rails, tips, programming
Posted
Wed 7 Mar, 2007
Comments
0

Verify used in my code

Have you found that any of your Rails projects get hits to controller methods that expect parameters? A neat way of handling this is the ActionController::Verification module.

By adding calls to verify in your controller, you can elegantly catch all kinds of unexpected but reasonable uses of your system: from mistyped URLs and browser history auto-complete to web crawlers blindly following links. This way, you can redirect people somewhere logical instead of flashing them with an error message.

You can even insert a message into the flash:

verify :params => "user",
       :only => :update_password,
       :add_flash => { "alert" => "Updating your password was not possible without the required information" },
       :redirect_to => :config_url

Security Code