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. Pingback: Josh's Blog » Blog Archive » T​e​s​t​i​n​g N​a​m​e​d R​o​u​t​e​s i​n t​h​e R​a​i​l​s C​o​n​s​o​l​e

  4. 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”

  5. Pingback: Testing Rails Routes in the Console | Sophwarist

  6. 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.