diff --git a/actionview/CHANGELOG.md b/actionview/CHANGELOG.md index 74b612927f..5a7b99959f 100644 --- a/actionview/CHANGELOG.md +++ b/actionview/CHANGELOG.md @@ -1,3 +1,8 @@ +* Yield translated strings to calls of `ActionView::FormBuilder#button` + when a block is given. + + *Sean Doyle* + * Alias `ActionView::Helpers::Tags::Label::LabelBuilder#translation` to `#to_s` so that `form.label` calls can yield that value to their blocks. diff --git a/actionview/lib/action_view/helpers/form_helper.rb b/actionview/lib/action_view/helpers/form_helper.rb index 4e681450db..faa855dc6a 100644 --- a/actionview/lib/action_view/helpers/form_helper.rb +++ b/actionview/lib/action_view/helpers/form_helper.rb @@ -2496,10 +2496,22 @@ module ActionView # # Ask me! # # # + # button do |text| + # content_tag(:strong, text) + # end + # # => + # def button(value = nil, options = {}, &block) value, options = nil, value if value.is_a?(Hash) value ||= submit_default_value - @template.button_tag(value, options, &block) + + if block_given? + value = @template.capture { yield(value) } + end + + @template.button_tag(value, options) end def emitted_hidden_id? # :nodoc: diff --git a/actionview/test/template/form_helper/form_with_test.rb b/actionview/test/template/form_helper/form_with_test.rb index a24c0b69f5..9bf6fb97e4 100644 --- a/actionview/test/template/form_helper/form_with_test.rb +++ b/actionview/test/template/form_helper/form_with_test.rb @@ -365,6 +365,18 @@ class FormWithActsLikeFormForTest < FormWithTest assert_dom_equal expected, output_buffer end + def test_form_with_button_yields_translation + form_with(model: @post) do |f| + concat(f.button { |value| concat content_tag(:span, value) }) + end + + expected = whole_form("/posts/123", method: :patch) do + "" + end + + assert_dom_equal expected, output_buffer + end + def test_form_with_not_outputting_ids old_value = ActionView::Helpers::FormHelper.form_with_generates_ids ActionView::Helpers::FormHelper.form_with_generates_ids = false @@ -981,9 +993,7 @@ class FormWithActsLikeFormForTest < FormWithTest form_with(model: Post.new) do |f| concat( f.label(:title) do |builder| - concat tag.span(builder, { - class: ("new_record" unless builder.object.persisted?) - }) + concat tag.span(builder, class: ("new_record" unless builder.object.persisted?)) end ) end