Using custom form builders with nested models in Ruby on Rails -


i'm using nested model form technique railscast 197 (asciicast here). i'm running problem setup, need use custom form builder 1 of nested models' partials. i've got working, sort of, modifiying link_to_add_fields_helper, so...

def link_to_add_fields(name, f, association)     new_object = f.object.class.reflect_on_association(association).klass.new     fields = f.fields_for(association, new_object, :child_index => "new_#{association}", :builder => answerformbuilder) |builder|   #was...   #fields = f.fields_for(association, new_object, :child_index => "new_#{association}") |builder|     render(association.to_s.singularize + "_fields", :f => builder)     end     link_to_function(name, h("add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")"))   end 

so see setting both nested models answerformbuilder kinda solved problem, it's not elegant since both questions , answers don't need them. additionally i've gotten point i'd application wide form builder. brings me second question, how can "nest" custom form builders? i.e. have answerformbuilder implement methods applicationformbuilder have, plus other special ones answers. much, i've looked everywhere , asked on other blogs no answer yet.

so figured out months later...

def link_to_add_fields(name, f, association, options = {})     new_object = f.object.class.reflect_on_association(association).klass.new     fields = f.fields_for(association, new_object, :child_index => "new_#{association}", :builder => options[:builder]) |builder|     render(association.to_s.singularize + "_fields", :f => builder)     end     link_to_function(name, h("add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")"))   end 

then when when need custom form builder call pass link_to_add_fields hash key :builder , value of specific builder.

link_to_add_fields "add question", f, :questions, :builder => questionformbuilder 

if no hash (or no :builder key value pair) passed link_add_fields fields_for method defaults regular form builder.


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -