Skip to content

Commit a4556b6

Browse files
Translate labels for belongs_to/_form if available (#2590)
Currently, if a model (e.g., `Customer`) has a translation (e.g., `Client`), the translated label is correctly displayed on the Show page. However, on the Edit and New pages, the original label (e.g., `Customer`) is displayed instead of the translated one. This commit ensures that the translated label for `BelongsTo` fields is consistently displayed across all pages. Specifically, it modifies the `belongs_to`/`_form` partial to display the translated label (if available) instead of the original one. Fixes #625.
1 parent 826fe48 commit a4556b6

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

app/views/fields/belongs_to/_form.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ that displays all possible records to associate with.
1717
%>
1818

1919
<div class="field-unit__label">
20-
<%= f.label field.permitted_attribute %>
20+
<%= f.label field.attribute, for: "#{f.object_name}_#{field.permitted_attribute}" %>
2121
</div>
2222
<div class="field-unit__field">
2323
<%= f.select(field.permitted_attribute,

spec/features/orders_form_spec.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,26 @@
2828
expected = order.customer.id.to_s
2929
expect(find_field("Customer").value).to eq expected
3030
end
31+
32+
it "displays translated label when translation for the attribute is available" do
33+
order = create(:order)
34+
custom_attribute_name = "Client"
35+
translations = {
36+
activerecord: {
37+
attributes: {
38+
order: {
39+
customer: custom_attribute_name
40+
}
41+
}
42+
}
43+
}
44+
45+
with_translations(:en, translations) do
46+
visit edit_admin_order_path(order)
47+
48+
expect(page).to have_css("label", text: custom_attribute_name)
49+
end
50+
end
3151
end
3252

3353
describe "has_many relationships" do
@@ -92,6 +112,26 @@
92112
end
93113
end
94114

115+
it "displays translated label when translation for the attribute is available" do
116+
order = create(:order)
117+
custom_attribute_name = "Lines"
118+
translations = {
119+
activerecord: {
120+
attributes: {
121+
order: {
122+
line_items: custom_attribute_name
123+
}
124+
}
125+
}
126+
}
127+
128+
with_translations(:en, translations) do
129+
visit edit_admin_order_path(order)
130+
131+
expect(page).to have_css("label", text: custom_attribute_name)
132+
end
133+
end
134+
95135
def find_option(associated_model, field_locator)
96136
field = find_field(field_locator)
97137
field.find("option", text: displayed(associated_model))

0 commit comments

Comments
 (0)