Took me a couple hours to figure this out.
When you do config.action_view.default_form_builder = MyFormBuilder in your appliaction.rb, it doesn't get autoloaded (ie. reloaded when you change something in development - which is really handy).
But, despite what this page says, changing it to say ActionView::Base.default_form_builder = MyFormBuilder in an initializer does squat to make my formbuilder get auto-reloaded.
It seems that the problem is that the class is never invoked by name, and so the autoloader doesn't kick in. If instead you say :builder => MyFormBuilder on the form_for tag, it does get reloaded.
If you insert the code <% MyFormBuilder %> at the top of your erb page, the class actually does get reloaded, but the form builder still uses the old version. A Class is actually an Object, and ActionView::Base.default_form_builder still has a pointer to the old Class object, even though the constant MyFormBuilder now points to the new class. At least that's what I think happens.
What ended up working for me was putting this code in config/initializers/set_default_form_builder.rb, which defines new form_for and fields_for tags, which sets the :builder option to the builder I want, invoked by name. NOW my formbuilder gets reloaded with each change, AND I don't have to specify it on each form.
Here's the complete code I'm using:
Ahh, much easier to develop it now.
Enjoy!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ActionView | |
module Helpers | |
module FormHelper | |
def form_for_with_bootstrap(record, options = {}, &proc) | |
options[:builder] = BootstrapFormBuilder | |
form_for_without_bootstrap(record, options, &proc) | |
end | |
def fields_for_with_bootstrap(record_name, record_object = nil, options = {}, &block) | |
options[:builder] = BootstrapFormBuilder | |
fields_for_without_bootstrap(record_name, record_object, options, &block) | |
end | |
alias_method_chain :form_for, :bootstrap | |
alias_method_chain :fields_for, :bootstrap | |
end | |
end | |
end |
Comments