Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to add dynamically generated fields in forms to the database rails
    primarykey
    data
    text
    <p>**Hello everyone I am trying to build one address book where a user can insert his details .</p> <ol> <li>A user can insert his information from the form , i am providing dynamic fields if he wants to add more then 1 info like a number can be of type home and personal. My Query is how to bind those dynamic values to the rails form and submit the data to the db.**</li> </ol> <p>My model look like a user can have an data which has info type info name and info data For Example a user can insert a <strong>infotype= Number infoname = Homephone infodata = 23623623</strong> a user can insert as many numbers he wants of different type.Please help<br> I am doing it like this</p> <pre><code> &lt;%= form_for @client_info, :validate =&gt; true do |f|%&gt; &lt;% if @client_info.errors.any? %&gt; &lt;div id="error_explanation", class="alert-message Warning"&gt; &lt;h2&gt;&lt;%= pluralize(@client.errors.count, "error") %&gt; Occured&lt;/h2&gt; &lt;ul&gt; &lt;% @client.errors.full_messages.each do |msg| %&gt; &lt;li&gt; &lt;%= msg %&gt; &lt;/li&gt; &lt;% end %&gt; &lt;/ul&gt; &lt;/div&gt; &lt;% end %&gt; &lt;select id = "opts" &gt; &lt;option value = "0"&gt;Select Option&lt;/option&gt; &lt;option value = "1"&gt;Phone Number&lt;/option&gt; &lt;option value = "2"&gt;Address&lt;/option&gt; &lt;option value = "3"&gt;Date&lt;/option&gt; &lt;option value = "4"&gt;Email&lt;/option&gt; &lt;option value = "5"&gt;Link&lt;/option&gt; &lt;/select&gt; &lt;form&gt; &lt;div id="ap"&gt;&lt;/div&gt; &lt;/form&gt; &lt;script type="text/javascript"&gt; $(document).ready(function() { $("#opts").change(function() { if($("#opts").val() == ("1")) { $("#ap").append('&lt;div id="phn"&gt;&lt;select id ="phn"&gt;&lt;option&gt;Personal&lt;/option&gt;&lt;option &gt;Home&lt;/option&gt;&lt;option &gt;Custom&lt;/option&gt;&lt;/select&gt; &lt;input type ="text" id="ad"&gt;&lt;/input&gt;' + '&lt;a href="#" class="remove1" id="rm" &gt;Remove&lt;/a&gt;&lt;/div&gt;'); } if($("#opts").val() == ("2")) { $("#ap").append('&lt;div id= "add"&gt;&lt;select id ="add"&gt;&lt;option&gt;Home Address&lt;/option&gt;&lt;option &gt;Work Address&lt;/option&gt;&lt;option &gt;Custom&lt;/option&gt;&lt;/select&gt; &lt;textarea rows="2" cols="20"&gt;&lt;/textarea&gt;' + '&lt;a href="#" class="remove2" id="rm" &gt;Remove&lt;/a&gt;&lt;/div&gt;'); } if($("#opts").val() == ("3")) { $("#ap").append('&lt;div id ="date"&gt;&lt;select id ="date"&gt;&lt;option&gt;Birthday&lt;/option&gt;&lt;option &gt;Anniversary&lt;/option&gt;&lt;option &gt;Custom&lt;/option&gt;&lt;/select&gt; &lt;input type ="text" id="datepic" &gt;&lt;/input&gt;' + '&lt;a href="#" class="remove3" id="rm" &gt;Remove&lt;/a&gt;&lt;/div&gt;'); } if($("#opts").val() == ("4")) { $("#ap").append('&lt;div id ="email"&gt;&lt;select id ="email"&gt;&lt;option&gt;Home Email&lt;/option&gt;&lt;option &gt;Work Email&lt;/option&gt;&lt;option &gt;Custom Email&lt;/option&gt;&lt;/select&gt; &lt;input type ="text"&gt;&lt;/input&gt;' + '&lt;a href="#" class="remove4" id="rm" &gt;Remove&lt;/a&gt;&lt;/div&gt;'); } if($("#opts").val() == ("5")) { $("#ap").append('&lt;div id="link"&gt;&lt;select id ="link"&gt;&lt;option&gt;Profle&lt;/option&gt;&lt;option &gt;Blog&lt;/option&gt;&lt;option&gt;Homepage&lt;/option&gt;&lt;option&gt;Custom&lt;/option&gt;&lt;/select&gt; &lt;input type ="text"&gt;&lt;/input&gt;' + '&lt;a href="#" class="remove5" id="rm" &gt;Remove&lt;/a&gt;&lt;/div&gt;'); } }); $('.remove1').live('click', function() { $("#phn").remove(); return false; }); }); &lt;/script&gt; &lt;div class="actions"&gt; &lt;%= f.submit :class =&gt; "btn primary" %&gt; &lt;a href="&lt;%= clients_path %&gt;" class="btn"&gt;Cancel&lt;/a&gt; &lt;/div&gt; &lt;% end %&gt; </code></pre> <p>Now i want to add this html generated fields to my rails form?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload