ruby on rails - Save multiple same named fields to database record -


i'm trying save multiple fields same name database record

this have now:

<%= form_for @complaint, url: {action: 'create'}, :html => {:multipart => true} |f| %>   <%= f.label :complaint_info, 'describa lo que sucedió' %>    <%= f.cktext_area :complaint_info, :class => 'someclass', :ckeditor => {:language => 'us', :toolbar => 'mini'}, tabindex: '-1' %>    <%= f.fields_for :witnesses |witnesses_form| %>         <%= witnesses_form.label :name, '¿hay testigos?' %>         <%= witnesses_form.text_field :name, placeholder: 'escriba el nombre del testigo', tabindex: '-1' %>         <%= witnesses_form.label :phone, 'número de teléfono del testigo' %>         <%= witnesses_form.text_field :phone, placeholder: 'escriba el número de teléfono' %>          <%= witnesses_form.label :name, '¿hay testigos?' %>         <%= witnesses_form.text_field :name, placeholder: 'escriba el nombre del testigo', tabindex: '-1' %>         <%= witnesses_form.label :phone, 'número de teléfono del testigo' %>         <%= witnesses_form.text_field :phone, placeholder: 'escriba el número de teléfono' %>   <% end %>   <% end %> 

in controller:

  def new     @complaint = complaint.new     @complaint.witnesses.build   end    def create     @complaint = current_user.complaints.build(complaint_params)     if @complaint.save        redirect_to dashboard_complaint_path(@complaint)     else       render 'new'     end   end    private    def complaint_params     params.require(:complaint).permit(:complaint_info, witnesses_attributes: [:name, :phone])   end 

what want give user option add multiple witnesses , save them each new record in database.

edit:

here models:

complaint:

class complaint < activerecord::base   belongs_to :user   has_many   :witnesses    accepts_nested_attributes_for :witnesses end 

and witness:

class witness < activerecord::base   belongs_to :complaint end 

edit2:

heres controller returns

started post "/dashboard/complaints" 127.0.0.1 @ 2015-06-10 12:14:10 -0400 processing dashboard::complaintscontroller#create html   parameters: {"utf8"=>"✓", "authenticity_token"=>"0ek0kaaydur1ktwuolceedag0fb63woijiixl7asm1zmbxwdhyccxce82ijpljrmnehs39um9eehl/gwjkpqiw==", "complaint"=>{"complaint_info"=>"<p>this description</p>\r\n", "witnesses_attributes"=>{"0"=>{"name"=>"second witness", "phone"=>"7877877877"}}}, "button"=>""}   user load (0.3ms)  select  "users".* "users" "users"."id" = $1  order "users"."id" asc limit 1  [["id", 1]]    (0.3ms)  begin   sql (0.8ms)  insert "complaints" ("complaint_info", "user_id", "created_at", "updated_at") values ($1, $2, $3, $4, $5, $6, $7, $8) returning "id"  [["complaint_info", "<p>this description</p>\r\n"], ["user_id", 1], ["created_at", "2015-06-10 16:14:10.452890"], ["updated_at", "2015-06-10 16:14:10.452890"]]   sql (1.3ms)  insert "witnesses" ("name", "phone", "complaint_id", "created_at", "updated_at") values ($1, $2, $3, $4, $5) returning "id"  [["name", "second witness"], ["phone", "7877877877"], ["complaint_id", 19], ["created_at", "2015-06-10 16:14:10.457497"], ["updated_at", "2015-06-10 16:14:10.457497"]]    (96.3ms)  commit redirected http://localhost:3000/dashboard/complaints/19 completed 302 found in 125ms (activerecord: 98.9ms) 

as can see, saves 1 witness record ["name", "second witness"] , last field (there 2 fields, typed first witness , second witness in last one).

well, here approach. want 2 records save. need :

def new   @complaint = complaint.new   2.times { @complaint.witnesses.build } end 

the change form :

<%= form_for @complaint, url: {action: 'create'}, :html => {:multipart => true} |f| %>   <%= f.label :complaint_info, 'describa lo que sucedió' %>    <%= f.cktext_area :complaint_info, :class => 'someclass', :ckeditor => {:language => 'us', :toolbar => 'mini'}, tabindex: '-1' %>    <%= f.fields_for :witnesses |witnesses_form| %>         <%= witnesses_form.label :name, '¿hay testigos?' %>         <%= witnesses_form.text_field :name, placeholder: 'escriba el nombre del testigo', tabindex: '-1' %>         <%= witnesses_form.label :phone, 'número de teléfono del testigo' %>         <%= witnesses_form.text_field :phone, placeholder: 'escriba el número de teléfono' %>   <% end %> <% end %> 

the block given nested fields_for call repeated each witnesses instance in associated f object.


Comments

Popular posts from this blog

javascript - oscilloscope of speaker input stops rendering after a few seconds -

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -