具有嵌套属性的Rails模型无法保存但不显示任何错误消息

时间:2016-11-24 23:54:37

标签: ruby-on-rails ruby nested-forms nested-form-for

我有以下Rails模型,它们通过多对多关系进行通信:

class Task < ApplicationRecord
  belongs_to :case
  has_many :task_actor_roles
  has_many :task_artifacts

  has_many :task_activities
  has_many :activities, through: :task_activities

  accepts_nested_attributes_for :task_activities

  attr_accessor :tasks_attributes
end

...

class TaskActivity < ApplicationRecord
  belongs_to :task
  belongs_to :activity
  belongs_to :instantiation_operator
end

...

class Activity < ApplicationRecord
  belongs_to :software_process
  has_many :artifacts, :dependent => :destroy
  belongs_to :activity_role
  has_many :task_activities
  has_many :tasks, through: :task_activities
end

我正在使用嵌套表单gem在新任务表单中添加多个选择字段。 这是生成表单的一段代码:

<%= nested_form_for(task, url: {action: action}) do |f| %>
  <%= f.label :activities %>
  <%= f.fields_for :task_activities do |task_activity| %>
    <%= task_activity.select(:activity_id,@activities.collect { |p| [p.name, p.id] } ) %>
    <%= task_activity.hidden_field :instantiation_operator_id, :value => @operator.id %>
    <%= task_activity.link_to_remove "Remove this activity" %>
  <% end %>
  <p><%= f.link_to_add "Add an activity", :task_activities %></p>
  <div class="field">
     <%= f.label :name %>
     <%= f.text_field :name %>
  </div>
  <%= f.submit %>
<%= end %>

当我发送数据时,页面会重新加载,就像它会显示验证错误一样,但是它没有显示任何内容。

这是终端日志的样子:

    Started POST "/tasks/create_group" for 127.0.0.1 at 2016-11-24 21:51:40 -0200
Processing by TasksController#create_group as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"[FILTERED]", "task"=>{"case_id"=>"1", "task_activities_attributes"=>{"1480031490821"=>{"activity_id"=>"11", "instantiation_operator_id"=>"6", "_destroy"=>"false"}, "1480031492604"=>{"activity_id"=>"14", "instantiation_operator_id"=>"6", "_destroy"=>"false"}}, "name"=>"Doris Woodard", "description"=>"Aperiam impedit aut fugit amet anim perspiciatis mollit", "priority"=>"Dolores explicabo Ipsum dolorem sit in cum iste enim nisi sint sed ratione odio", "category"=>"Eos cumque consequuntur iste est enim minim beatae et dolores mollit id quis ut occaecat laboriosam molestiae aut ipsa nulla", "status"=>"Veritatis qui id quaerat distinctio Blanditiis odio sit quisquam vel provident optio consectetur", "startDate"=>"25-Feb-2001", "comments"=>"Voluptatem eiusmod amet in ut", "TTC"=>"43"}, "task_activity"=>{"instantiation_operator_id"=>"6"}, "task_actor_role"=>{"task_role_id"=>"2", "task_actor_id"=>"3"}, "commit"=>"Create Task"}
   (0.1ms)  BEGIN
  Case Load (0.3ms)  SELECT  "cases".* FROM "cases" WHERE "cases"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  TaskActor Load (0.4ms)  SELECT  "task_actors".* FROM "task_actors" WHERE "task_actors"."id" = $1 LIMIT $2  [["id", 3], ["LIMIT", 1]]
  TaskRole Load (0.3ms)  SELECT  "task_roles".* FROM "task_roles" WHERE "task_roles"."id" = $1 LIMIT $2  [["id", 2], ["LIMIT", 1]]
  Activity Load (0.2ms)  SELECT  "activities".* FROM "activities" WHERE "activities"."id" = $1 LIMIT $2  [["id", 11], ["LIMIT", 1]]
  InstantiationOperator Load (0.4ms)  SELECT  "instantiation_operators".* FROM "instantiation_operators" WHERE "instantiation_operators"."id" = $1 LIMIT $2  [["id", 6], ["LIMIT", 1]]
  Activity Load (0.4ms)  SELECT  "activities".* FROM "activities" WHERE "activities"."id" = $1 LIMIT $2  [["id", 14], ["LIMIT", 1]]
  CACHE (0.0ms)  SELECT  "instantiation_operators".* FROM "instantiation_operators" WHERE "instantiation_operators"."id" = $1 LIMIT $2  [["id", 6], ["LIMIT", 1]]
   (0.2ms)  ROLLBACK
  InstantiationOperator Load (0.2ms)  SELECT  "instantiation_operators".* FROM "instantiation_operators" WHERE "instantiation_operators"."name" = $1 LIMIT $2  [["name", "group"], ["LIMIT", 1]]
  Case Load (0.3ms)  SELECT  "cases".* FROM "cases" WHERE "cases"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  Rendering tasks/group.html.erb within layouts/application
  Project Load (0.2ms)  SELECT  "projects".* FROM "projects" WHERE "projects"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  SoftwareProcess Load (0.2ms)  SELECT  "software_processes".* FROM "software_processes" WHERE "software_processes"."id" = $1 LIMIT $2  [["id", 2], ["LIMIT", 1]]
  TaskRole Load (0.3ms)  SELECT "task_roles".* FROM "task_roles"
  TaskActor Load (0.2ms)  SELECT "task_actors".* FROM "task_actors"
  Activity Load (0.2ms)  SELECT "activities".* FROM "activities"
  Rendered tasks/_form.html.erb (10.8ms)
  Rendered tasks/group.html.erb within layouts/application (14.1ms)
  User Load (0.7ms)  SELECT  "users".* FROM "users" WHERE "users"."remember_token" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["remember_token", "e97f4b0c90c219f7223b805989eb25dbc6e2cae2"], ["LIMIT", 1]]
Completed 200 OK in 220ms (Views: 150.4ms | ActiveRecord: 7.7ms)

当我在表单中注释掉具有嵌套属性的行时,它会保存而不会出现任何问题。所以问题就在于此。

更新

所以我用byebug来检查@task:

@task.valid?
  Case Load (0.4ms)  SELECT  "cases".* FROM "cases" WHERE "cases"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
  TaskActor Load (0.2ms)  SELECT  "task_actors".* FROM "task_actors" WHERE "task_actors"."id" = $1 LIMIT $2  [["id", 3], ["LIMIT", 1]]
  TaskRole Load (0.2ms)  SELECT  "task_roles".* FROM "task_roles" WHERE "task_roles"."id" = $1 LIMIT $2  [["id", 2], ["LIMIT", 1]]
  Activity Load (0.3ms)  SELECT  "activities".* FROM "activities" WHERE "activities"."id" = $1 LIMIT $2  [["id", 2], ["LIMIT", 1]]
  InstantiationOperator Load (0.2ms)  SELECT  "instantiation_operators".* FROM "instantiation_operators" WHERE "instantiation_operators"."id" = $1 LIMIT $2  [["id", 3], ["LIMIT", 1]]
  Activity Load (0.2ms)  SELECT  "activities".* FROM "activities" WHERE "activities"."id" = $1 LIMIT $2  [["id", 7], ["LIMIT", 1]]
  CACHE (0.0ms)  SELECT  "instantiation_operators".* FROM "instantiation_operators" WHERE "instantiation_operators"."id" = $1 LIMIT $2  [["id", 3], ["LIMIT", 1]]
false

...

@task.errors
#<ActiveModel::Errors:0x007fcd984174c8 @base=#<Task id: nil, name: "Forrest Wright", description: "In dolores voluptatibus maiores in fugit fuga Quis...", priority: "Voluptatem modi quod consequatur vel sed est exerc...", category: "Sequi vitae at explicabo In", status: "Omnis quo sed obcaecati voluptate corporis cumque ...", startDate: "1986-07-04", comments: "Ut fugit dicta voluptatem Ullam voluptas id et", TTC: 86, endDate: nil, created_at: nil, updated_at: nil, case_id: 1>, @messages={:"task_activities.task"=>["must exist"]}, @details={"task_activities.task"=>[{:error=>:blank}]}>

2 个答案:

答案 0 :(得分:0)

您必须将模型错误记录到production.log。实现这一目标的一种方法是:

def your_action
  logger.debug @task.errors
  [...]
end

或使用byebug(Rails 5+)或调试器(Rails 4 - )

进行调试
def your_action
  [...]
  byebug
  [...]
end

在rails服务器中,您可以检查@task变量。

答案 1 :(得分:0)

解决方案是在, inverse_of: :task前添加has_many :task_activities。 问题是任务活动试图在任务之前保存。我使用了byebug(感谢@ vl3的抬头)并检查了@ save.errors。我找到了消息:

@messages={:"task_activities.task"=>["must exist"]}

并且没有任务task_id被分配给任务活动。

我添加的这一行只是让任务在任务活动之前创建。