Skip to content
This repository was archived by the owner on Jan 18, 2025. It is now read-only.

Commit 10115aa

Browse files
committed
Add Test Out URL And Path Helpers In The Console as a Rails til
1 parent dc251ec commit 10115aa

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
1010

1111
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
1212

13-
_1168 TILs and counting..._
13+
_1169 TILs and counting..._
1414

1515
---
1616

@@ -765,6 +765,7 @@ _1168 TILs and counting..._
765765
- [Temporarily Disable strong_params](rails/temporarily-disable-strong-params.md)
766766
- [Test If An Instance Variable Was Assigned](rails/test-if-an-instance-variable-was-assigned.md)
767767
- [Test If deliver_later Is Called For A Mailer](rails/test-if-deliver-later-is-called-for-a-mailer.md)
768+
- [Test Out URL And Path Helpers In The Console](rails/test-out-url-and-path-helpers-in-the-console.md)
768769
- [Truncate Almost All Tables](rails/truncate-almost-all-tables.md)
769770
- [Update Column Versus Update Attribute](rails/update-column-versus-update-attribute.md)
770771
- [Upgrading Your Manifest For Sprocket's 4](rails/upgrading-your-manifest-for-sprockets-4.md)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Test Out URL And Path Helpers In The Console
2+
3+
Rails has fancy metaprogrammed URL and path helpers generated from the
4+
`config/routes.rb` file. There is a ton of configurability to these routes. It
5+
can sometimes be hard to know exactly how they'll behave or what the generated
6+
route helper will look like. In those cases, we may want to test them out in
7+
the console.
8+
9+
The Rails console doesn't have the same things autoloaded as mailers and views
10+
where we tend use these route helpers. So, we can reference them through
11+
`Rails.application.routes.url_helpers`.
12+
13+
From there we can run both `*_path` route helpers.
14+
15+
```ruby
16+
> Rails.application.routes.url_helpers
17+
.api_v1_post_with_slugged_title_path(
18+
slug: 123,
19+
slugged_title: 'a-recent-path'
20+
)
21+
22+
=> "/api/v1/posts/123/a-recent-path"
23+
```
24+
25+
and `*_url` path helpers.
26+
27+
```ruby
28+
> Rails.application.routes.url_helpers
29+
.api_v1_post_with_slugged_title_url(
30+
slug: 123,
31+
slugged_title: 'a-recent-path'
32+
)
33+
34+
=> "http://localhost:3000/api/v1/posts/123/a-recent-path"
35+
```
36+
37+
For the `*_url` path helpers, make sure you have [`default_url_options`
38+
set](set-default-url-options-for-entire-application.md).

0 commit comments

Comments
 (0)