I have a custom form builder for my application:
file location: /lib/ruby/app_form_builder.rb
class ActionView::Helpers::AppFormBuilder < ActionView::Helpers::FormBuilder
include ActionView::Helpers::TagHelper
include ActionView::Context
def foo
‘bar’
end
end
``
I am requiring it in an initializer:
require “#{Rails.root}/lib/ruby/app_form_builder.rb”
ActionView::Base.default_form_builder = ActionView::Helpers::AppFormBuilder
``
This works, but I have to restart the server on every change. I was reading the Constant Guide, but didn’t find the right way to made this file to be automaticaly reloaded. I tried to put this in /app/helpers forlder and making it default in application.rb, but I got “unitialized constant” error.
Also, I would like to know if Rails can reload code that modify native Ruby classes:
class String
def do_something
‘something’
end
end
``
I have a custom form builder for my application:
I am requiring it in an initializer:
require “#{Rails.root}/lib/ruby/app_form_builder.rb”
ActionView::Base.default_form_builder = ActionView::Helpers::AppFormBuilder
``
This works, but I have to restart the server on every change. I was reading the Constant Guide, but didn’t find the right way to made this file to be automaticaly reloaded. I tried to put this in /app/helpers forlder and making it default in application.rb, but I got “unitialized constant” error.
require is never a good idea if you want something to be reloaded. You’ve also got a clash between the class name and it’s location on disk - having the class just be AppFormBuilder in lib/app_form_builder.rb should work.
Lastly if you look into the bowels of action view, the default_form_builder method looks like this
def default_form_builder
builder = ActionView::Base.default_form_builder
builder.respond_to?(:constantize) ? builder.constantize : builder
end
So if you set default_form_builder to be a “AppFormBuilder” then that should allow it to be reloaded.
Also, I would like to know if Rails can reload code that modify native Ruby classes:
class String
def do_something
‘something’
end
end
``
This can’t be done. code reloading is done by removing the class/module (using remove_const) and then letting it be loaded again, which wouldn’t work for a class like String
Fred
I couldn’t make this work with “default_form_builder” method.
My solution was using a custom “form_for” method:
application_helper.rb
def app_form_for(object, options = {})
options.merge!(builder: AppFormBuilder)
form_for object, options do |f|
yield f
end
end
``
And adding the Builder folder on autoload_paths.
@Frederick Cheung: Thank you for your help!