Console: Verify Routes

Frequently we stub out basic concepts and tests in the ubiquitous Rails console. On the fly it is useful to know if a route exists when working with paths. For instance does contacts_path(3) actually map to a meaningful path? You can use some route classes to make your console provide the information. When you start your console session add these two includes:


 :001 > include ActionDispatch::Routing
 => Object
 :002 > include Rails.application.routes.url_helpers
 => Object

First lets try a path we know does not exist:


 :003 > monkeys_path(3)
NoMethodError: undefined method `monkeys_path' for main:Object

Now lets use a real path:


 :004 > email_path(4)
 => "/emails/4/edit"

Perhaps a look at the routes will be helpful at this point:


rake routes|grep email|grep edit

edit_email GET      /emails/:id/edit(.:format)         emails#edit

This completes verification of the path. So who remembers to throw those includes into the console after it starts up? Certainly not me. We can use an .irbrc file and place it in the home (~) directory:


echo "include ActionDispatch::Routing" >> ~/.irbrc
echo "include Rails.application.routes.url_helpers" >> ~/.irbrc

Now you have the .irbrc file for other useful includes prior to console startup.