Rails 4, change the default form builder worldwide

I have a sub-classified default form builder to add some additional methods. Sample code below.

module ApplicationHelper class AppFormBuilder < ActionView::Helpers::FormBuilder def coordinates_field(method, options = {}) options[:readonly] = 'true' .... @template.text_field(@object_name, method, objectify_options(options)) end end end 

This works well, but to use it I need to change the view code for each form using the coordinates_field method, i.e.

 <%= form_for @object, :builder => ApplicationHelper::AppFormBuilder do |f| %> ... <% end %> 

It seems theoretically possible to change the default form builder around the world ( config.action_view.default_form_builder ), but I can't get it to work. This is what I tried in /config/application.rb :

 module Web class Application < Rails::Application ... config.action_view.default_form_builder = "ApplicationHelper::AppFormBuilder" end end 

Which results in an undefined error for the "new" method for "ApplicationHelper :: AppFormBuilder": String when I hit a view that has a form.

If I try instead

 config.action_view.default_form_builder = ApplicationHelper::AppFormBuilder 

I get the error * config.action_view.default_form_builder = ApplicationHelper :: AppFormBuilder * when the application starts.

Can anyone give directions on how to make this work?

+6
source share
4 answers

ApplicationHelper::AppFormBuilder is not yet required at the time application.rb is loaded. You can try putting this in a separate initialization file (create it in config \ initializers):

 module Web class Application ActionView::Base.default_form_builder = AppFormBuilder end end 
+3
source

As mentioned in white papers, in Rails 5 the right way is to specify it in the controller. To make it widely used, simply install in your ApplicationController :

 class ApplicationController < ActionController::Base default_form_builder AppFormBuilder end 

That makes a lot more sense. FormHelpers are part of the presentation layer, not the configuration of the application.

+6
source

I like Max answer, but (rails n00b here is so YMMV) I find this to be equivalent and cleaner, right in config / application.rb:

 config.after_initialize do ActionView::Base.default_form_builder = MyCustomHelper::MyCustomFormBuilder end 

obviously you are replacing the names MyCustomHelper and MyCustomFormBuilder.

took me about 48 hours to figure this out thanks to all this well-structured rail documentation.

+3
source

You should be able to set this from the initializer, as with other configuration parameters. Create the default_form_builder.rb file in config/initializers/ . The syntax should be simpler than the @Max answer.

 Rails.application.config.action_view.default_form_builder = AppFormBuilder 

I suggest you not include this in an assistant. Add it as a separate class to the /lib directory. You may need a prefix with the module in which it is contained.

Finally, you can install globally from config/application.rb , but you will have to pass it as a string, since the class may not be loaded when the rails start.

 config.action_view.default_form_builder = 'AppFormBuilder' 
+1
source

All Articles