0

i have the following code:

 <%= form_for Contact.new do |f| %>
   <%= f.text_field :name, placeholder: 'Name', style: 'width : 15ex' %>
   <%= f.text_field :phone, placeholder: 'Phone', style: 'width : 15ex' %>
   <%= f.text_field :note, placeholder: 'optional notes', style: 'width : 15ex' %>
   <%= submit_tag "Add", class: "btn btn-info pull-right" %>
 <% end %>

this works fine for one Contact, but if i want to create two objects by clicking on the add button its not working, here is the code for two contacts.

<%= form_for Contact.new do |f| %>
      <%= f.text_field :name, placeholder: 'Name', style: 'width : 15ex' %>
      <%= f.text_field :phone, placeholder: 'Phone', style: 'width : 15ex' %>
      <%= f.text_field :note, placeholder: 'optional notes', style: 'width : 15ex' %>
<% end %>
<%= form_for Contact.new do |f| %>
      <%= f.text_field :name, placeholder: 'Name', style: 'width : 15ex' %>
      <%= f.text_field :phone, placeholder: 'Phone', style: 'width : 15ex' %>
      <%= f.text_field :note, placeholder: 'optional notes', style: 'width : 15ex' %>
      <%= submit_tag "Add", class: "btn btn-info pull-right" %> 
<% end %>

I hope you can help me.

Fry
  • 922
  • 2
  • 10
  • 24
  • If look at the generated HTML code, you'll see two forms, and a submit button can only submit one, that's probably why it's not working. But you should provide more information about WHAT is not working though, like error message, output, ... – Antoine Jan 22 '14 at 12:03
  • Ok, so what would be the best way to generate two objects and submit it with one button? – Fry Jan 22 '14 at 12:03
  • Everything is working fine, the problem is only that it creates only 1 object, the first one, the second one doesnt get created. – Fry Jan 22 '14 at 12:10
  • @Antoine, that question doesn't provide an answer to the problem here. It's only about `<% form_for %>` vs `<%= form_for %>`. – Mischa Jan 22 '14 at 12:16
  • Right, but the answer to the OP's question is actually there, I guess that's the main point – Antoine Jan 22 '14 at 12:21
  • i looked at the Link Mischa postet, the link that @Antoine posted does not answer my question, its about displaying multiple objects within one form, i want to save/create two objects at once (submit) within a form, not displaying it. – Fry Jan 22 '14 at 12:23
  • @Fry you probably want to use javascript or ajax to do this, you would have something like add another contact documentation: http://edgeguides.rubyonrails.org/working_with_javascript_in_rails.html – bjhaid Jan 22 '14 at 12:39
  • ok, probably everyone is thinking to difficult. Just imagine the following: In the html i want to give the user the option to create two instead of one contacts, if he wants two, he just needs to fill out both textfields and click add. Thats all i want. – Fry Jan 22 '14 at 12:47
  • 1
    User nested attribute and nested form for you can add resources dynamically – Pramod Shinde Jan 22 '14 at 13:09

1 Answers1

0

You can handle this in two ways:

  1. Using AJAX to submit dynamically the forms (maybe with something like this).
  2. Use a custom submit method that iterates over an array of objects. Check the question Antoine posted on the comments (link for consistency) and note how the form receives params for user[].

GL & HF.

Community
  • 1
  • 1
rlecaro2
  • 755
  • 7
  • 14