I am trying to update an array held in the database however the update action is not working. The array is not updated and no error is shown. Other parameters in the form are correctly updated in the database.
A form sends an array as one of the parameters (in this case responses: []
):
"task"=> {"title"=>"tasko uno", "responses"=>["yes", "no", "maybe", "other"] ... }
The update action:
def update
@task = Task.find(params[:id])
if @task.update(tasks_params)
redirect_to edit_task_path
else
render 'new'
end
end
The params permissions:
private
def tasks_params
params.require(:task).permit(:title, :help, :task_type, :taskflow_id, :responses => [])
end
The database is the out the box sql-lite and the Task model has serialize :responses, Array
, the array column is of type text
, with a column title responses
.
Any help would be much appreciated.
EDIT The migration file:
class CreateTasks < ActiveRecord::Migration
def change
create_table :tasks do |t|
t.string :title
t.integer :task_type
t.text :help
t.text :responses
t.belongs_to :taskflow
t.timestamps null: false
end
end
end
Update action server log:
Started PATCH "/tasks/1" for 127.0.0.1 at 2016-06-28 12:37:12 +0100
Processing by TasksController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"BL2fI3JDIGfTqWxQHInR/NFI6N/agETaUb97lAZ86PpNmpj1ofQ+TRkHZNdiISyOMlag3geleig6PsaqaWNr8Q==", "task"=>{"title"=>"tasko uno", "help"=>"some help text", "task_type"=>"2", "taskflow_id"=>"1"}, "responses"=>["test1", "test2"], "commit"=>"Update Task", "id"=>"1"}
Task Load (0.1ms) SELECT "tasks".* FROM "tasks" WHERE "tasks"."id" = ? LIMIT 1 [["id", 1]]
(0.0ms) begin transaction
(0.0ms) commit transaction
Taskflow Load (0.1ms) SELECT "taskflows".* FROM "taskflows" WHERE "taskflows"."id" = ? LIMIT 1 [["id", 1]]
Redirected to http://localhost:3000/taskflows/1/edit
Completed 302 Found in 3ms (ActiveRecord: 0.2ms)
The form - I'm trying to have a form that adds fields dynamically:
<%= bootstrap_form_for @task do |f| %>
<%= f.text_field :title %>
<%= f.text_area :help %>
<%= f.text_field :task_type %>
<%= f.hidden_field :taskflow_id, value: @taskflow.id %>
<hr>
<p><strong>Responses</strong></p>
<div class="admin-task-responses">
<% if @task.responses %>
<% @task.responses.each do |r| %>
<%= render "tasks/task_response", {:res => r} %>
<% end %>
<% end %>
</div>
<div class="btn btn-primary add-response-btn">
<span class="glyphicon glyphicon-plus"></span> Add Response
</div>
<%= f.submit %>
<% end %>
The partial:
<% if local_assigns.has_key? :res %>
<div class="task-response form-group">
<%= text_field_tag "responses[]", '', :class => 'form-control', :value => res %>
</div>
<% end %>