1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-05 08:08:02 +00:00

Add Test Out URL And Path Helpers In The Console as a Rails til

This commit is contained in:
jbranchaud
2021-12-06 16:11:04 -06:00
parent dc251ecead
commit 10115aa1cd
2 changed files with 40 additions and 1 deletions

View File

@@ -0,0 +1,38 @@
# Test Out URL And Path Helpers In The Console
Rails has fancy metaprogrammed URL and path helpers generated from the
`config/routes.rb` file. There is a ton of configurability to these routes. It
can sometimes be hard to know exactly how they'll behave or what the generated
route helper will look like. In those cases, we may want to test them out in
the console.
The Rails console doesn't have the same things autoloaded as mailers and views
where we tend use these route helpers. So, we can reference them through
`Rails.application.routes.url_helpers`.
From there we can run both `*_path` route helpers.
```ruby
> Rails.application.routes.url_helpers
.api_v1_post_with_slugged_title_path(
slug: 123,
slugged_title: 'a-recent-path'
)
=> "/api/v1/posts/123/a-recent-path"
```
and `*_url` path helpers.
```ruby
> Rails.application.routes.url_helpers
.api_v1_post_with_slugged_title_url(
slug: 123,
slugged_title: 'a-recent-path'
)
=> "http://localhost:3000/api/v1/posts/123/a-recent-path"
```
For the `*_url` path helpers, make sure you have [`default_url_options`
set](set-default-url-options-for-entire-application.md).