Fix crash on attributes with interpolation arguments

The application crashed when we generated hints to attributes with
interpolation arguments in their `human_attribute_name`.

When generating the hint, we used the `custom_label` method to generate
a label and get the `for` attribute and, since we weren't passing a
text, it used the default human attribute name for the field. However,
it crashes if the default attribute name requires an interpolation
argument.

So now, since we were only using the `custom_label` method in order to
get the `for` attribute, we're simply passing an arbitrary text to the
method.
This commit is contained in:
Javi Martín
2022-03-25 15:23:09 +01:00
parent d719b2291e
commit 1fa05b1f54
2 changed files with 13 additions and 1 deletions

View File

@@ -83,7 +83,7 @@ class ConsulFormBuilder < FoundationRailsHelper::FormBuilder
def help_text_id(attribute, options)
if options[:hint].present?
"#{custom_label(attribute, nil, nil).match(/for="([^"]+)"/)[1]}-help-text"
"#{custom_label(attribute, "Example", nil).match(/for="([^"]+)"/)[1]}-help-text"
end
end
end

View File

@@ -40,6 +40,18 @@ describe ConsulFormBuilder do
expect(page).not_to have_css "input[hint]"
end
describe "attributes requiring interpolation parameters" do
before { I18n.backend.store_translations(:en, { attributes: { title: "Title %{info}" }}) }
after { I18n.backend.reload! }
it "generates a hint" do
render builder.text_field(:title, label: "Title whatever", hint: "Make it quick")
expect(page).to have_css ".help-text", text: "Make it quick"
expect(page).to have_css "input[aria-describedby='dummy_title-help-text']"
end
end
end
describe "#select" do