diff --git a/README.md b/README.md index 86b1a2d..bc5ce1b 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ pairing with smart people at Hashrocket. For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186). -_1168 TILs and counting..._ +_1169 TILs and counting..._ --- @@ -765,6 +765,7 @@ _1168 TILs and counting..._ - [Temporarily Disable strong_params](rails/temporarily-disable-strong-params.md) - [Test If An Instance Variable Was Assigned](rails/test-if-an-instance-variable-was-assigned.md) - [Test If deliver_later Is Called For A Mailer](rails/test-if-deliver-later-is-called-for-a-mailer.md) +- [Test Out URL And Path Helpers In The Console](rails/test-out-url-and-path-helpers-in-the-console.md) - [Truncate Almost All Tables](rails/truncate-almost-all-tables.md) - [Update Column Versus Update Attribute](rails/update-column-versus-update-attribute.md) - [Upgrading Your Manifest For Sprocket's 4](rails/upgrading-your-manifest-for-sprockets-4.md) diff --git a/rails/test-out-url-and-path-helpers-in-the-console.md b/rails/test-out-url-and-path-helpers-in-the-console.md new file mode 100644 index 0000000..1923c39 --- /dev/null +++ b/rails/test-out-url-and-path-helpers-in-the-console.md @@ -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).