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!
Em quinta-feira, 15 de janeiro de 2015 10:28:32 UTC-2, Frederick Cheung
escreveu:
>
>
> On Thursday, January 15, 2015 at 11:51:39 AM UTC, Andrey Nering wrote:
>>
>> 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
>> <http://edgeguides.rubyonrails.org/autoloading_and_reloading_constants.html>,
>>
>> 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
>
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/rubyonrails-talk/88572db8-eb7d-4b1a-8884-b01844876091%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.