Skip to content

Commit a04c061

Browse files
committed
Bugfix config.action_view.default_form_builder option
1 parent 2cdd229 commit a04c061

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

actionview/lib/action_view/helpers/form_helper.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1935,6 +1935,8 @@ def nested_child_index(name)
19351935
end
19361936

19371937
ActiveSupport.on_load(:action_view) do
1938-
cattr_accessor(:default_form_builder) { ::ActionView::Helpers::FormBuilder }
1938+
cattr_accessor(:default_form_builder, instance_writer: false, instance_reader: false) do
1939+
::ActionView::Helpers::FormBuilder
1940+
end
19391941
end
19401942
end

railties/test/application/configuration_test.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,45 @@ def index
494494
assert last_response.body =~ /csrf\-param/
495495
end
496496

497+
test "default form builder specified as a string" do
498+
499+
app_file 'config/initializers/form_builder.rb', <<-RUBY
500+
class CustomFormBuilder < ActionView::Helpers::FormBuilder
501+
def text_field(attribute, *args)
502+
label(attribute) + super(attribute, *args)
503+
end
504+
end
505+
Rails.configuration.action_view.default_form_builder = "CustomFormBuilder"
506+
RUBY
507+
508+
app_file 'app/models/post.rb', <<-RUBY
509+
class Post
510+
include ActiveModel::Model
511+
attr_accessor :name
512+
end
513+
RUBY
514+
515+
516+
app_file 'app/controllers/posts_controller.rb', <<-RUBY
517+
class PostsController < ApplicationController
518+
def index
519+
render inline: "<%= begin; form_for(Post.new) {|f| f.text_field(:name)}; rescue => e; e.to_s; end %>"
520+
end
521+
end
522+
RUBY
523+
524+
add_to_config <<-RUBY
525+
routes.prepend do
526+
resources :posts
527+
end
528+
RUBY
529+
530+
require "#{app_path}/config/environment"
531+
532+
get "/posts"
533+
assert_match(/label/, last_response.body)
534+
end
535+
497536
test "default method for update can be changed" do
498537
app_file 'app/models/post.rb', <<-RUBY
499538
class Post

0 commit comments

Comments
 (0)