Note that there are some explanatory texts on larger screens.

plurals
  1. PORails insert dynamic fields into the database
    primarykey
    data
    text
    <p>I have a simple app with a single model (task) and a single attribute (name).</p> <h1>app/views/tasks/_form.html.erb</h1> <pre><code>&lt;%= form_tag :action =&gt; 'create' %&gt; &lt;div id="dynamicInput"&gt; Task Name &lt;input type="text" name="task[name][]"&gt; &lt;/div&gt; &lt;input type="button" value="Add New Tag" onClick="addInput('dynamicInput');"&gt; &lt;%= submit_tag "Create"%&gt; </code></pre> <h1>app/assets/javascripts/application.js</h1> <pre><code>var counter = 1; function addInput(divName){ var newdiv = document.createElement('div'); newdiv.innerHTML = "Task Name &lt;input type='text' name='task[name][]'&gt;"; document.getElementById(divName).appendChild(newdiv); counter++; } </code></pre> <p>The code above allows me to add as many fields as I like. I am able to enter a single name, but if I dynamically add 2 fields, here are what my params look like...</p> <pre><code>Started POST "/tasks" for 127.0.0.1 at 2013-07-25 12:36:57 -0400 Processing by TasksController#create as HTML Parameters: {"utf8"=&gt;"✓", "authenticity_token"=&gt;"PnTQnxM3GFRtqkXi09jLH8UJBiaRCI0chSZ716cVWJ0=", "task"=&gt;{"name"=&gt;["name1", "name2"]}, "commit"=&gt;"Create"} (0.1ms) begin transaction SQL (5.6ms) INSERT INTO "tasks" ("created_at", "name", "updated_at") VALUES (?, ?, ?) [["created_at", Thu, 25 Jul 2013 16:36:57 UTC +00:00], ["name", ["name1", "name2"]], ["updated_at", Thu, 25 Jul 2013 16:36:57 UTC +00:00]] (153.1ms) commit transaction Redirected to http://localhost:3000/tasks/1 Completed 302 Found in 165ms (ActiveRecord: 158.7ms) </code></pre> <p>I want a separate task/name for each name I enter.</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.
 

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