Skip to content

Commit 8b3ef48

Browse files
committed
Add Apply Basic HTML Formatting To Block Of Text as a Rails TIL
1 parent c2184a5 commit 8b3ef48

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-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-
_1570 TILs and counting..._
13+
_1571 TILs and counting..._
1414

1515
See some of the other learning resources I work on:
1616
- [Ruby Operator Lookup](https://www.visualmode.dev/ruby-operators)
@@ -954,6 +954,7 @@ See some of the other learning resources I work on:
954954
- [All or Nothing Database Transactions](rails/all-or-nothing-database-transactions.md)
955955
- [Alphabetize Schema Columns To Keep Them Consistent](rails/alphabetize-schema-columns-to-keep-them-consistent.md)
956956
- [Alter The Rails Setup Script](rails/alter-the-rails-setup-script.md)
957+
- [Apply Basic HTML Formatting To Block Of Text](rails/apply-basic-html-formatting-to-block-of-text.md)
957958
- [Assert Two Arrays Have The Same Items With RSpec](rails/assert-two-arrays-have-the-same-items-with-rspec.md)
958959
- [Attach A File With Capybara](rails/attach-a-file-with-capybara.md)
959960
- [Attribute Getter without the Recursion](rails/attribute-getter-without-the-recursion.md)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Apply Basic HTML Formatting To Block Of Text
2+
3+
My Rails app has a form that allows a user to enter in free-form text. I enter
4+
in a couple paragraphs and save the record. It is rendered on a show page with
5+
a couple lines of ERB like so:
6+
7+
```ruby
8+
<div class="max-w-3xl mx-auto">
9+
<div class="space-y-4">
10+
<div class="prose mt-8 text-gray-700">
11+
<%= @record.notes %>
12+
</div>
13+
</div>
14+
</div>
15+
```
16+
17+
When I view the erb-displayed version of that record's text, all those
18+
carefully spaced paragraphs are clumped together. That is because those newline
19+
(`\n` and `\n\n`) characters while understood to be whitespace do not have
20+
formatting implications in the browser like a combination of HTML tags and CSS
21+
do.
22+
23+
I can apply some basic formatting with [the aptly named `simple_format` method
24+
available as an `ActionView`
25+
helper](https://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#method-i-simple_format).
26+
27+
```ruby
28+
<div class="max-w-3xl mx-auto">
29+
<div class="space-y-4">
30+
<div class="prose mt-8 text-gray-700">
31+
<%= simple_format(@record.notes) %>
32+
</div>
33+
</div>
34+
</div>
35+
```
36+
37+
This turns single `\n` characters into a `<br />` tag and double `\n\n` cause
38+
the surrounding paragraphs to be wrapped in `<p>` tags. That simple formatting
39+
combined with my existing TailwindCSS styles makes the formatting of my text
40+
immediately look much better.

0 commit comments

Comments
 (0)