Skip to content

Commit aa71ff5

Browse files
committed
Add Override Text Displayed By Form Label as a Rails TIL
1 parent 48278c4 commit aa71ff5

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-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-
_1572 TILs and counting..._
13+
_1573 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)
@@ -1032,6 +1032,7 @@ See some of the other learning resources I work on:
10321032
- [Migrating Up Down Up](rails/migrating-up-down-up.md)
10331033
- [Mock Rails Environment With An Inquiry Instance](rails/mock-rails-environment-with-an-inquiry-instance.md)
10341034
- [Order Matters For `rescue_from` Blocks](rails/order-matters-for-rescue-from-blocks.md)
1035+
- [Override Text Displayed By Form Label](rails/override-text-displayed-by-form-label.md)
10351036
- [Params Includes Submission Button Info](rails/params-includes-submission-button-info.md)
10361037
- [Params Is A Hash With Indifferent Access](rails/params-is-a-hash-with-indifferent-access.md)
10371038
- [Parse Query Params From A URL](rails/parse-query-params-from-a-url.md)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Override Text Displayed By Form Label
2+
3+
Rails does a good job with the default text displayed by a form label. It takes
4+
the primary symbol value you give it and capitalizes that. And that is often
5+
good enough.
6+
7+
```ruby
8+
<%= form_with(model: post) do |form| %>
9+
<%= form.label :title, class: "text-sm font-medium text-gray-700" %>
10+
<%= form.text_field :title, required: true, class: "..." %>
11+
<% end %>
12+
```
13+
14+
This will yield a label value of _Title_.
15+
16+
Sometimes, however, the casing needs to be different or you need entirely
17+
different text. Take this URL field for example. Rails will convert `:url` into
18+
_Url_ for the label text. Not ideal. I can override the default with a second
19+
positional argument, in this case, `"URL"`.
20+
21+
```ruby
22+
<%= form_with(model: post) do |form| %>
23+
<%= form.label :url, "URL", class: "text-sm font-medium text-gray-700" %>
24+
<%= form.url_field :url, required: true, class: "..." %>
25+
<% end %>
26+
```
27+
28+
The [Rails docs have another good
29+
example](https://guides.rubyonrails.org/form_helpers.html#a-generic-search-form).
30+
A label with a value of `query` that is overridden to display "Search for:".
31+
32+
```ruby
33+
<%= form_with url: "/search", method: :get do |form| %>
34+
<%= form.label :query, "Search for:" %>
35+
<%= form.search_field :query %>
36+
<%= form.submit "Search" %>
37+
<% end %>
38+
```

0 commit comments

Comments
 (0)