Rails数据库没有在更新操作中更新数组



我正在尝试更新数据库中保存的数组,但是更新操作不工作。数组不更新,也不显示错误。表格中的其他参数在数据库中被正确更新。

表单发送一个数组作为参数之一(在本例中为responses: []):

"task"=> {"title"=>"tasko uno", "responses"=>["yes", "no", "maybe", "other"] ... }

更新操作:

def update
    @task = Task.find(params[:id])
    if @task.update(tasks_params)
      redirect_to edit_task_path
    else
      render 'new'
    end
  end

参数权限:

private
    def tasks_params
      params.require(:task).permit(:title, :help, :task_type, :taskflow_id, :responses => [])
    end

数据库为现成的sql-lite, Task模型为serialize :responses, Array,数组列类型为text,列标题为responses

任何帮助都将非常感激。

编辑迁移文件:

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

更新操作服务器日志:

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)

的形式-我试图有一个动态添加字段的形式:

<%= 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 %>
分:

<% if local_assigns.has_key? :res %>
<div class="task-response form-group">
  <%= text_field_tag "responses[]", '', :class => 'form-control', :value => res %>
</div>
<% end %>

您的"responses"=>["test1", "test2"]tasks散列之外,但另一方面,您的responses字段是任务模型的一部分。在你的表格中,你很可能会使用"…"因此,它没有连接到您的@task,这是不保存或至少没有得到任何错误的原因。

如果您需要更多的帮助,请将您的表单添加到问题中。

或者,您可以尝试以下操作,但我不建议您调整格式:

params[:task][:responses] = params[:responses]

这应该为您工作:

<%= text_field_tag "task[responses][]", '', :class => 'form-control', :value => res %>

这将是更好的,如果你存储json格式的"响应",所以在json编码,并在需要时解码它回来。

代码应该是这样的

private
params[:task][:responses] = params[:task][:responses].to_json
    def tasks_params
      params.require(:task).permit(:title, :help, :task_type, :taskflow_id,   
      :responses )
    end

最新更新