352

Rails defines a bunch of magic with named routes that make helpers for your routes. Sometimes, especially with nested routes, it can get a little confusing to keep track of what URL you'll get for a given route helper method call. Is it possible to, using the Ruby console, see what link a given helper function will generate? For example, given a named helper like post_path(post) I want to see what URL is generated.

7 Answers 7

499

You can show them with rake routes directly.

In a Rails console, you can call app.post_path. This will work in Rails ~= 2.3 and >= 3.1.0.

Sign up to request clarification or add additional context in comments.

5 Comments

Just in a follow up of my own comment, seems this is possible from rails 3 console, in case you're using. First, stick a fake request into your app object, by calling something like app.get "/" then just instance_eval the wanted methods, as they are now protected by default. Something like: app.instance_eval{ post_path(post) }
Chubas' comment above worked in Rails 3. This is very useful when writing tests so I don't have to wait 1 minute to find out that the route is not correct. Also, it is great to test get and post calls. You don't need to call app.get first.
app.teh_path still works in Rails 4.0 and is useful for separating engine paths from main application paths.
If you're mounting an engine, e.g. mount Spree::Core::Engine, :at => '/', then you'd access the paths via the engine name, like app.spree_core_engine.some_path. Or, if the "engine_name" is configured to be something different like in this code then you'd do app.spree.some_path.
I had to add the host parameter like so: app.article_url(my_article, host: 'mydomain.com')
450

you can also

include Rails.application.routes.url_helpers

from inside a console sessions to access the helpers:

url_for controller: :users, only_path: true
users_path
# => '/users'

or

Rails.application.routes.url_helpers.users_path

6 Comments

This is much easier than the above solution IMO
this is the right answer to the original question in my opinion
This should be the best answer IMO
I can't be bothered to remember this, so I just come back here every couple days to copy/paste. Thanks.
Couldn't you also reference the url_helpers directly from the console, like Rails.application.routes.url_helpers.users_path?
|
37

In the Rails console, the variable app holds a session object on which you can call path and URL helpers as instance methods.

app.users_path

Comments

24

You can always check the output of path_helpers in console. Just use the helper with app

app.post_path(3)
#=> "/posts/3"

app.posts_path
#=> "/posts"

app.posts_url
#=> "http://www.example.com/posts"

Comments

4

Remember if your route is name-spaced, Like:

product GET  /products/:id(.:format)  spree/products#show

Then try :

helper.link_to("test", app.spree.product_path(Spree::Product.first), method: :get)

output

Spree::Product Load (0.4ms)  SELECT  "spree_products".* FROM "spree_products"  WHERE "spree_products"."deleted_at" IS NULL  ORDER BY "spree_products"."id" ASC LIMIT 1
=> "<a data-method=\"get\" href=\"/products/this-is-the-title\">test</a>" 

1 Comment

Thanks for the spree example, you're an angel that fell from heaven.
2

From rails 6 rake routes doesn't exist.

You can run instead

rails routes

Comments

1

For Rails 5.2.4.1, I had to

app.extend app._routes.named_routes.path_helpers_module
app.whatever_path

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.