diff options
author | claudiob <claudiob@gmail.com> | 2014-09-26 20:11:39 -0700 |
---|---|---|
committer | claudiob <claudiob@gmail.com> | 2014-11-20 12:46:30 -0800 |
commit | b0cc858dd71576011c6f2a3995457c070d8b55d5 (patch) | |
tree | 890ffd92d4c7b1d164950e22cb9469097ccaffbd /actionview/test | |
parent | b0f44c9ff0be84ebc42760029adeeb9dd954655f (diff) | |
download | rails-b0cc858dd71576011c6f2a3995457c070d8b55d5.tar.gz rails-b0cc858dd71576011c6f2a3995457c070d8b55d5.tar.bz2 rails-b0cc858dd71576011c6f2a3995457c070d8b55d5.zip |
Add `:enforce_utf8` option to form_for
Since 06388b0 `form_tag` accepts the option `enforce_utf8` which, when set to
false, prevents the hidden "UTF8 enforcer" field from appearing in the output.
This commit implements the same behavior for `form_for`.
Stems from https://github.com/rails/rails/pull/17685#issuecomment-63871395
Diffstat (limited to 'actionview/test')
-rw-r--r-- | actionview/test/template/form_helper_test.rb | 36 |
1 files changed, 33 insertions, 3 deletions
diff --git a/actionview/test/template/form_helper_test.rb b/actionview/test/template/form_helper_test.rb index 4bbbdf4fb1..a131752b2a 100644 --- a/actionview/test/template/form_helper_test.rb +++ b/actionview/test/template/form_helper_test.rb @@ -1864,6 +1864,30 @@ class FormHelperTest < ActionView::TestCase assert_dom_equal expected, output_buffer end + def test_form_for_enforce_utf8_true + form_for(:post, enforce_utf8: true) do |f| + concat f.text_field(:title) + end + + expected = whole_form("/", nil, nil, enforce_utf8: true) do + "<input name='post[title]' type='text' id='post_title' value='Hello World' />" + end + + assert_dom_equal expected, output_buffer + end + + def test_form_for_enforce_utf8_false + form_for(:post, enforce_utf8: false) do |f| + concat f.text_field(:title) + end + + expected = whole_form("/", nil, nil, enforce_utf8: false) do + "<input name='post[title]' type='text' id='post_title' value='Hello World' />" + end + + assert_dom_equal expected, output_buffer + end + def test_form_for_with_remote_in_html form_for(@post, url: '/', html: { remote: true, id: 'create-post', method: :patch }) do |f| concat f.text_field(:title) @@ -3313,8 +3337,14 @@ class FormHelperTest < ActionView::TestCase protected - def hidden_fields(method = nil) - txt = %{<input name="utf8" type="hidden" value="✓" />} + def hidden_fields(options = {}) + method = options[:method] + + if options.fetch(:enforce_utf8, true) + txt = %{<input name="utf8" type="hidden" value="✓" />} + else + txt = '' + end if method && !%w(get post).include?(method.to_s) txt << %{<input name="_method" type="hidden" value="#{method}" />} @@ -3338,7 +3368,7 @@ class FormHelperTest < ActionView::TestCase method, remote, multipart = options.values_at(:method, :remote, :multipart) - form_text(action, id, html_class, remote, multipart, method) + hidden_fields(method) + contents + "</form>" + form_text(action, id, html_class, remote, multipart, method) + hidden_fields(options.slice :method, :enforce_utf8) + contents + "</form>" end def protect_against_forgery? |