mirror of
https://github.com/jbranchaud/til
synced 2026-01-02 22:58:01 +00:00
Add Use .ruby Extension For Template File as a Rails TIL
This commit is contained in:
@@ -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)
|
||||
|
||||
42
rails/use-ruby-extension-for-template-file.md
Normal file
42
rails/use-ruby-extension-for-template-file.md
Normal file
@@ -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.
|
||||
Reference in New Issue
Block a user