@@ -33,6 +33,12 @@ def nested_partial_with_form_builder
33
33
end
34
34
end
35
35
36
+ class ValidatingPost < Post
37
+ include ActiveModel ::Validations
38
+
39
+ validates :title , presence : true
40
+ end
41
+
36
42
class TestController < ActionController ::Base
37
43
protect_from_forgery
38
44
@@ -487,6 +493,14 @@ def partial_with_form_builder
487
493
render partial : ActionView ::Helpers ::FormBuilder . new ( :post , nil , view_context , { } )
488
494
end
489
495
496
+ def partial_with_form_builder_and_invalid_model
497
+ post = ValidatingPost . new
498
+
499
+ post . validate
500
+
501
+ render partial : ActionView ::Helpers ::FormBuilder . new ( :post , post , view_context , { } )
502
+ end
503
+
490
504
def partial_with_form_builder_subclass
491
505
render partial : LabellingFormBuilder . new ( :post , nil , view_context , { } )
492
506
end
@@ -680,6 +694,7 @@ class RenderTest < ActionController::TestCase
680
694
get :partial_only , to : "test#partial_only"
681
695
get :partial_with_counter , to : "test#partial_with_counter"
682
696
get :partial_with_form_builder , to : "test#partial_with_form_builder"
697
+ get :partial_with_form_builder_and_invalid_model , to : "test#partial_with_form_builder_and_invalid_model"
683
698
get :partial_with_form_builder_subclass , to : "test#partial_with_form_builder_subclass"
684
699
get :partial_with_hash_object , to : "test#partial_with_hash_object"
685
700
get :partial_with_locals , to : "test#partial_with_locals"
@@ -1300,6 +1315,44 @@ def test_partial_with_form_builder
1300
1315
assert_equal "<label for=\" post_title\" >Title</label>\n " , @response . body
1301
1316
end
1302
1317
1318
+ def test_partial_with_form_builder_and_invalid_model
1319
+ get :partial_with_form_builder_and_invalid_model
1320
+
1321
+ assert_equal <<~HTML . strip , @response . body . strip
1322
+ < div class ="field_with_errors "> < label for ="post_title "> Title</ label > </ div >
1323
+ HTML
1324
+ end
1325
+
1326
+ def test_partial_with_form_builder_and_invalid_model_custom_field_error_proc
1327
+ old_proc = ActionView ::Base . field_error_proc
1328
+ ActionView ::Base . field_error_proc = proc { |html | tag . div html , class : "errors" }
1329
+
1330
+ get :partial_with_form_builder_and_invalid_model
1331
+
1332
+ assert_equal <<~HTML . strip , @response . body . strip
1333
+ < div class ="errors "> < label for ="post_title "> Title</ label > </ div >
1334
+ HTML
1335
+ ensure
1336
+ ActionView ::Base . field_error_proc = old_proc if old_proc
1337
+ end
1338
+
1339
+ def test_partial_with_form_builder_and_invalid_model_custom_rendering_field_error_proc
1340
+ old_proc = ActionView ::Base . field_error_proc
1341
+ ActionView ::Base . field_error_proc = proc do |html_tag , instance |
1342
+ render inline : <<~ERB , locals : { html_tag : html_tag , instance : instance }
1343
+ < div class ="field_with_errors "> <%= html_tag %> < span class ="error "> <%= [ instance . error_message ] . join ( ', ' ) %> </ span > </ div >
1344
+ ERB
1345
+ end
1346
+
1347
+ get :partial_with_form_builder_and_invalid_model
1348
+
1349
+ assert_equal <<~HTML . strip , @response . body . strip
1350
+ < div class ="field_with_errors "> < label for ="post_title "> Title</ label > < span class ="error "> can't be blank</ span > </ div >
1351
+ HTML
1352
+ ensure
1353
+ ActionView ::Base . field_error_proc = old_proc if old_proc
1354
+ end
1355
+
1303
1356
def test_partial_with_form_builder_subclass
1304
1357
get :partial_with_form_builder_subclass
1305
1358
assert_equal "<label for=\" post_title\" >Title</label>\n " , @response . body
0 commit comments