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.

This entry was posted in Programming and tagged , , , . Bookmark the permalink.

14 Responses to Testing Named Routes in the Rails Console

  1. Ryan says:

    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. mtyaka says:

    Thank you very much, just what I was looking for!

  3. Joe Ellis says:

    Exactly what I needed, thanks!

  4. Carlos says:

    Big thanks

  5. Max Williams says:

    Just what i was looking for, thanks :)

  6. Phil says:

    Thank you kindly.

  7. lucapette says:

    nice tip. I’m using it at this very moment

  8. insanedreamer says:

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

    include Rails.application.routes.url_helpers

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

  10. Joshua Emmons says:

    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″

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

  12. Turadg says:

    Joshua, that’s even better! Thanks.

  13. Peter Baker says:

    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

  14. Abhilash says:

    This was a big help! Thanks very much.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>