Autoloading custom form builder #3341

Closed
ghost opened this Issue Oct 16, 2011 · 5 comments

Comments

Projects
None yet
3 participants
@ghost

ghost commented Oct 16, 2011

Custom form builder is not autoloaded for some reason.

I always get the same error telling that:

D:\Web\eclipse_workspace\intech_crm>rails s
C:/Ruby192/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake.rb:2503:in `const_missin
g': uninitialized constant IntechCrm::Application::IntechFormBuilder (NameError)

    from D:/Web/eclipse_workspace/intech_crm/config/application.rb:29:in `<c

lass:Application>'
from D:/Web/eclipse_workspace/intech_crm/config/application.rb:12:in <m odule:IntechCrm>' from D:/Web/eclipse_workspace/intech_crm/config/application.rb:11:in<t
op (required)>'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.1.1/lib/rails/comman
ds.rb:52:in require' from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.1.1/lib/rails/comman ds.rb:52:inblock in <top (required)>'
from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.1.1/lib/rails/comman
ds.rb:49:in tap' from C:/Ruby192/lib/ruby/gems/1.9.1/gems/railties-3.1.1/lib/rails/comman ds.rb:49:in<top (required)>'
from script/rails:6:in require' from script/rails:6:in

'

application.rb

config.autoload_paths += [Rails.root.join('lib'), Rails.root.join('lib', 'validators')]
config.action_view.default_form_builder = IntechFormBuilder

lib/intech_form_builder.rb

class IntechFormBuilder < ActionView::Helpers::FormBuilder
end

I am running:
ruby 1.9.2p290 (2011-07-09) [i386-mingw32]
Rails 3.1.1

Contributor

mauricio commented Oct 16, 2011

Placing this configuration at an initializer file (instead of application.rb) will work.

@ghost

ghost commented Oct 16, 2011

Yes, it seems to be working now. Thanks a lot! I've closed the ticket, but could you expalin it in more detail why it should to be placed just in initializer file?

@ghost ghost closed this Oct 16, 2011

@ghost ghost reopened this Oct 16, 2011

@ghost

ghost commented Oct 16, 2011

It has started working right after i added this code:

module IntechCrm
  class Application
    #config.action_view.default_form_builder = IntechFormBuilder - This code doesn't work.
    ActionView::Base.default_form_builder = IntechFormBuilder
  end
end
Contributor

mauricio commented Oct 16, 2011

At application.rb Rails is still setting up the environment to accept your configurations and setting the load path in there won't work because it will not be set at that moment, that's why you should be using initializer files for this, as when the initializer file is run, everything is ready to accept your changes.

And as you have noticed, config.action_view.default_form_builder is not exactly like calling ActionView::Base.default_form_builder, what happens is what you place at config.action_view.default_form_builder is set at ActionView::Base.default_form_builder after the application.rb file is executed.

Contributor

calvincorreli commented Nov 10, 2011

This solution did not work for me with Rails 3.1.1.

For future reference for others bumping their heads against the same problem, here's what did end up working for me:

http://pinds.com/2011/11/08/how-to-set-a-default-form-builder-in-rails-3-1-while-letting-it-be-autoloaded/

drogus added a commit to drogus/rails that referenced this issue Apr 7, 2012

@drogus drogus closed this in e6ab0d5 Apr 29, 2012

arunagw pushed a commit to arunagw/rails that referenced this issue Apr 29, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment