Reputation: 15365
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.
Upvotes: 349
Views: 155117
Reputation: 23661
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"
Upvotes: 24
Reputation: 1378
From rails 6 rake routes
doesn't exist.
You can run instead
rails routes
Upvotes: 2
Reputation: 1003
For Rails 5.2.4.1, I had to
app.extend app._routes.named_routes.path_helpers_module
app.whatever_path
Upvotes: 1
Reputation: 4591
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
Upvotes: 446
Reputation: 118271
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>"
Upvotes: 4
Reputation: 369
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
Upvotes: 36
Reputation: 18043
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.
Upvotes: 496