From 83d55c420e54fa72a81bf21b8218b95447cb5909 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Thu, 27 Nov 2025 10:29:56 -0600 Subject: [PATCH] Add Use .ruby Extension For Template File as a Rails TIL --- README.md | 3 +- rails/use-ruby-extension-for-template-file.md | 42 +++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 rails/use-ruby-extension-for-template-file.md diff --git a/README.md b/README.md index f50efe7..5255b9a 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). -_1700 TILs and counting..._ +_1701 TILs and counting..._ See some of the other learning resources I work on: @@ -1196,6 +1196,7 @@ If you've learned something here, support my efforts writing daily TILs by - [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) - [Use IRB And Ruby Flags With Rails Console](rails/use-irb-and-ruby-flags-with-rails-console.md) +- [Use .ruby Extension For Template File](rails/use-ruby-extension-for-template-file.md) - [Useful ActiveSupport Constants For Durations](rails/useful-active-support-constants-for-durations.md) - [Validate Column Data With Check Constraints](rails/validate-column-data-with-check-constraints.md) - [Verify And Read A Signed Cookie Value](rails/verify-and-read-a-signed-cookie-value.md) diff --git a/rails/use-ruby-extension-for-template-file.md b/rails/use-ruby-extension-for-template-file.md new file mode 100644 index 0000000..63dff08 --- /dev/null +++ b/rails/use-ruby-extension-for-template-file.md @@ -0,0 +1,42 @@ +# Use .ruby Extension For Template File + +An interesting feature of Rails that I can't seem to find documented anywhere is +that you can write a template file with plain Ruby by using the `.ruby` +extension. For instance, you might want to render some JSON from a template. +Instead of using `jbuilder` or `erb`, you can have a `show.json.ruby` file. This +is also popular with Turbo Stream files -- e.g. `update.turbo_stream.ruby`. + +How this works is that the entire file is evaluated as if it were a `.rb` file. +Then the return value of the final statement is what is returned and rendered by +Rails. + +```ruby +author_byline = @book.authors.map(&:name).to_sentence + +data = { + id: @book.id, + title: @book.title, + author: author_byline, + status: @book.published_at > Time.current ? 'Coming Soon' : 'Published', + publication_year: @book.published_at.year +} + +data.to_json +``` + +That final line converts the hash of data that we've built up into a JSON string +that can then be rendered by the controller action that corresponds to this view +template. + +Similarly, you can have a Turbo Stream template `show.turbo_stream.ruby` that +looks something like this: + +```ruby +[ + turbo_stream.prepend("posts", @post), + turbo_stream.update("form", partial: "form", locals: { post: Post.new }) +].join +``` + +This template file is made up of a single statement which is an array of turbo +stream results that get joined together.