File tree Expand file tree Collapse file tree 2 files changed +42
-1
lines changed Expand file tree Collapse file tree 2 files changed +42
-1
lines changed Original file line number Diff line number Diff line change @@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
10
10
11
11
For a steady stream of TILs, [ sign up for my newsletter] ( https://crafty-builder-6996.ck.page/e169c61186 ) .
12
12
13
- _ 1570 TILs and counting..._
13
+ _ 1571 TILs and counting..._
14
14
15
15
See some of the other learning resources I work on:
16
16
- [ Ruby Operator Lookup] ( https://www.visualmode.dev/ruby-operators )
@@ -954,6 +954,7 @@ See some of the other learning resources I work on:
954
954
- [ All or Nothing Database Transactions] ( rails/all-or-nothing-database-transactions.md )
955
955
- [ Alphabetize Schema Columns To Keep Them Consistent] ( rails/alphabetize-schema-columns-to-keep-them-consistent.md )
956
956
- [ 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 )
957
958
- [ Assert Two Arrays Have The Same Items With RSpec] ( rails/assert-two-arrays-have-the-same-items-with-rspec.md )
958
959
- [ Attach A File With Capybara] ( rails/attach-a-file-with-capybara.md )
959
960
- [ Attribute Getter without the Recursion] ( rails/attribute-getter-without-the-recursion.md )
Original file line number Diff line number Diff line change
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.
You can’t perform that action at this time.
0 commit comments