【问题标题】:Rails database not updating array in update actionRails 数据库未在更新操作中更新数组
【发布时间】:2016-06-26 16:01:54
【问题描述】:

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

表单发送一个数组作为参数之一(在本例中为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,任务模型有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 %>

【问题讨论】:

  • @gen 谢谢,但我已经尝试过了 - 我在模型中使用 serialize :responses 并检查了 responses 是复数形式。它没有抛出错误,所以我很难找到正在发生的事情。
  • 请添加您的服务器日志以进行更新操作。你知道怎么用撬吗?您应该在更新后检查您的对象。
  • 同时发布你的迁移,并尝试在没有 tasks_params 方法的情况下传递参数。
  • @gen 谢谢,我已经添加了迁移文件和更新操作的日志。在没有 task_params 的情况下传递参数会引发禁止属性错误。我会试一试。

标签: ruby-on-rails database activerecord


【解决方案1】:

您的"responses"=&gt;["test1", "test2"] 不在tasks 散列中,但另一方面,您的responses 字段是任务模型的一部分。最有可能的是,在您的表单中,您正在使用“..._tag”助手来输入您的回复,因此它没有连接到您的@task,这是不保存或至少没有收到任何错误的原因。

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

或者,您可以尝试以下方法,但我不建议您调整表格:

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

这应该适合你:

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

【讨论】:

  • 啊。谢谢!我在部分中使用 text_field_tag 来创建一个根据需要添加字段的动态表单。如果您能帮助我以正确的方式执行此操作,我已经发布了表格。
  • @RobotEyes;我更新了我的答案,为您提供了一个可能的解决方案。
【解决方案2】:

如果您将“响应”存储在 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

【讨论】:

  • 谢谢,但不幸的是数据库仍然没有更新
  • 这可能是你没有在attr_accessor中添加这个字段,就像在模型attr_accessor中一样:responses
  • 你也从响应中删除了[],它应该只是:responses,另外,这个字段在db中的数据类型是什么?它应该是字符串,文本..
  • 数据库中的数据类型是text,列名是responses。我应该向模型添加 attr_accessor 值吗?
  • 是的,你应该在模型中添加 attr_accessor :responses
猜你喜欢
  • 1970-01-01
  • 2017-08-31
  • 2016-07-28
  • 1970-01-01
  • 2012-12-24
  • 1970-01-01
  • 1970-01-01
  • 2023-03-31
  • 1970-01-01
相关资源
最近更新 更多