ruby on rails - Multiple records on one form -


i create form has format:

shared fields fields unique record [add record button] 

where [add] button can clicked many times necessary create

shared fields fields unique record fields unique record b fields unique record c [add record button] 

submitting form creates record a, b , c

back end

this creates 3 record, each shared , unique attributes per fields in form.

update

one thing shared each belong "employer". should form_for @employer?

thinking out loud, hold shared attributes attr_accessors on parent, , assign them children in "child#create" action of child's controller.

since you're filling in multiple models in 1 form without nesting, can't generate form form_for @something, since you've got no single object fill in.

i review database structure see if there way extract shared fields anto separate model. if there no way cleanly, read on.

rails helpers rendering object forms lay out fields in way rails can correctly parse hash. which, in turn, can used construct object. using this:

form_for @thing |f|   t.text_field :name end 

you form thing[name] field, in params so:

{..., thing: {name: "hello world"}, ...} 

see the guides more details on this.

and here's catch: don't have single object fill in. can neglect , build form still parsed nested hash. have fill in manually rails otherwise guess: form url , object name.

form_for :thing, url: thing_path |f|   f.text_field :name end 

if want create group of fields, use fields_for helper inside form. more info in documentation, in form this:

form_for :thing, url: thing_url |f|   f.text_field :name   f.fields_for :something |s|     s.text_field :value   end end 

this should render fieldset parsed single hash. may need array of nested fieldsets in case, see documentation.

ultimately get:

  • hash
    • shared parameters
    • array of objects
    • object a
    • object a
    • object b
    • ...

and ultimate problem – need add fields form through javascript, should know in advance markup add. there many ways tackle problem, 1 rendering example form each unique model , sampling using javascript.

edit: coincidentally, needed find out how render "bare" (not backed object) form can copied javascript , array of objects in parameters when multiple forms given.

the tricky part is, if use fields_for :something, yield form single object, not array of them. digging around, found seemingly undocumented feature (digging around code this post). it's used (i use haml/slim syntax brevity):

= form_for :thing, url: whatever |f|   = f.fields_for :stuff, index: '' |s| #<< `index: ''` part     = s.text_field :v 

semantically appears mean following: make form contains field stuff, filled in 1 or many fieldsets empty index. under hood, generates bit awkward field names @ first glance:

thing[stuff][v]   # before, without ` index: '' ` thing[stuff][][v] # after, see empty index? 

the fun part of can clone resultig fieldset without modifying it, , rails (or rack?) resolve set of forms separate object each. relies on browser preserving order of fields, true of time.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -