Testing Named Routes in the Rails Console

I finally found out how to do this, from the Rails Routing shortcut by David Black. In the Rails console, do this:

include ActionController::UrlWriter
default_url_options[:host] = 'whatever'

Then you can call your named route methods directly from the console.

14 Replies to “Testing Named Routes in the Rails Console”

  1. Interesting…but as a side note, I just noticed that the pdf you referenced has been included in The Rails Way book. Just some info for anyone considering purchasing that pdf like I did.

  2. For those of you who find this and are using Rails3, ActionController::UrlWriter has been deprecated. Instead use:

    include Rails.application.routes.url_helpers

  3. An even easier way (that still works in Rails3), just call your named route off of app:

    app.my_route_name_url(1) #=> “http://http://www.example.com/my_route_name/1”

  4. Very cool. Now I have a quick way to look at all the automagic paths and urls! Thanks all

    #from rails console
    app.methods.grep(/(path|url)/).sort

Comments are closed.