From aa71ff5f8ba775d6e888a7fe07e1239b2705ae19 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Fri, 24 Jan 2025 12:36:46 -0600 Subject: [PATCH] Add Override Text Displayed By Form Label as a Rails TIL --- README.md | 3 +- .../override-text-displayed-by-form-label.md | 38 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 rails/override-text-displayed-by-form-label.md diff --git a/README.md b/README.md index 57fbd3a..2ea6869 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). -_1572 TILs and counting..._ +_1573 TILs and counting..._ See some of the other learning resources I work on: - [Ruby Operator Lookup](https://www.visualmode.dev/ruby-operators) @@ -1032,6 +1032,7 @@ See some of the other learning resources I work on: - [Migrating Up Down Up](rails/migrating-up-down-up.md) - [Mock Rails Environment With An Inquiry Instance](rails/mock-rails-environment-with-an-inquiry-instance.md) - [Order Matters For `rescue_from` Blocks](rails/order-matters-for-rescue-from-blocks.md) +- [Override Text Displayed By Form Label](rails/override-text-displayed-by-form-label.md) - [Params Includes Submission Button Info](rails/params-includes-submission-button-info.md) - [Params Is A Hash With Indifferent Access](rails/params-is-a-hash-with-indifferent-access.md) - [Parse Query Params From A URL](rails/parse-query-params-from-a-url.md) diff --git a/rails/override-text-displayed-by-form-label.md b/rails/override-text-displayed-by-form-label.md new file mode 100644 index 0000000..65a1177 --- /dev/null +++ b/rails/override-text-displayed-by-form-label.md @@ -0,0 +1,38 @@ +# Override Text Displayed By Form Label + +Rails does a good job with the default text displayed by a form label. It takes +the primary symbol value you give it and capitalizes that. And that is often +good enough. + +```ruby +<%= form_with(model: post) do |form| %> + <%= form.label :title, class: "text-sm font-medium text-gray-700" %> + <%= form.text_field :title, required: true, class: "..." %> +<% end %> +``` + +This will yield a label value of _Title_. + +Sometimes, however, the casing needs to be different or you need entirely +different text. Take this URL field for example. Rails will convert `:url` into +_Url_ for the label text. Not ideal. I can override the default with a second +positional argument, in this case, `"URL"`. + +```ruby +<%= form_with(model: post) do |form| %> + <%= form.label :url, "URL", class: "text-sm font-medium text-gray-700" %> + <%= form.url_field :url, required: true, class: "..." %> +<% end %> +``` + +The [Rails docs have another good +example](https://guides.rubyonrails.org/form_helpers.html#a-generic-search-form). +A label with a value of `query` that is overridden to display "Search for:". + +```ruby +<%= form_with url: "/search", method: :get do |form| %> + <%= form.label :query, "Search for:" %> + <%= form.search_field :query %> + <%= form.submit "Search" %> +<% end %> +```