1

I have a simple app with a single model (task) and a single attribute (name).

app/views/tasks/_form.html.erb

<%= form_tag :action => 'create' %>
<div id="dynamicInput">
          Task Name <input type="text" name="task[name][]">
     </div>
     <input type="button" value="Add New Tag" onClick="addInput('dynamicInput');">
<%= submit_tag "Create"%>

app/assets/javascripts/application.js

var counter = 1;
function addInput(divName){
  var newdiv = document.createElement('div');
  newdiv.innerHTML = "Task Name <input type='text' name='task[name][]'>";
  document.getElementById(divName).appendChild(newdiv);
  counter++;
}

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...

Started POST "/tasks" for 127.0.0.1 at 2013-07-25 12:36:57 -0400
Processing by TasksController#create as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"PnTQnxM3GFRtqkXi09jLH8UJBiaRCI0chSZ716cVWJ0=", "task"=>{"name"=>["name1", "name2"]}, "commit"=>"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)

I want a separate task/name for each name I enter.

Mark Locklear
  • 5,044
  • 1
  • 51
  • 81
  • 2
    I think field should be named `task[name][]`. Why are you not using Rails form helpers? - life's way easier with them. – Mike Szyndel Jul 25 '13 at 15:24
  • is name set as an accessible attribute on your model? – muttonlamb Jul 25 '13 at 15:40
  • Thanks Michael, making that change allowed me to submit a single task name. I updated my question. Now if I insert a 2nd name, I do not get two separate tasks. Ideas? Also, I am open to form helpers. – Mark Locklear Jul 25 '13 at 16:16

1 Answers1

0

In Rails 4, the Strong Parameters feature requires that you whitelist params inside your controller. Also, according to the answer here, an array of params can't be whitelisted without a further tweak, so you need

params.require(:task).permit(name: [])
Community
  • 1
  • 1
bgates
  • 2,333
  • 1
  • 18
  • 12
  • Good call as I was using Rails 4 in addition to Rails 3 for testing this out. The real problem I am trying to solve to with the JavaScript. – Mark Locklear Jul 25 '13 at 16:18
  • Are you making multiple *tasks*, or multiple *tags* on one task? – bgates Jul 25 '13 at 16:24
  • Sorry about that...wrong console grab. Fixed above. So in the submission above I entered task 1 and task 2, but only got a singe task entered into the db with the name name1 - name2. what I want to 2 separate tasks...name1, and another task, name2 – Mark Locklear Jul 25 '13 at 16:40
  • 1
    In that case, you need to iterate through the task[:name] array in the controller: `task[:name].each{|individual_name| Task.create(name: individual_name)}` – bgates Jul 25 '13 at 16:45
  • That works dude. I will update your answer and mark as correct. Thanx! – Mark Locklear Jul 25 '13 at 17:10