How to set a default form builder in Rails 3.1 while letting it be autoloaded
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!
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!
About Calvin Correli
I've spent the last 17 years learning, growing, healing, and discovering who I truly am, so that I'm now living every day aligned with my life's purpose.
2 comments
Leave a comment