I多対多の関係を介して通信し、次のRailsのモデルを持っている: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
私は、新しいタスクフォームで複数選択フィールドを追加するために、ネストされた形の宝石を使用しています。ここ はフォームを生成するコードの一部です:
<%= 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)
:
これは、端末のログがどのように見えるかです。問題は確かにそれにあります。
UPDATE
だから私は@taskを検査するbyebug使用:
@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}]}>
ありがとうございました!私はbyebugを使用していて、@ task.task_activitiesがタスクactivitiyインスタンスを期待どおりに返すことを発見しました。アクティビティとインスタンス化演算子IDがありますが、タスクIDはありません。 なぜそれが起こっているのか? – Leandro
task.saveを呼び出し、エラー(task.errors)を確認する必要があります。レコードに検証エラーがある場合、そのレコードは保存されません。これを達成する他の方法は、task.validを使用することです。タスク。エラー。これはあなたのレコードをデータベースに保存しようとはしません。 – vl3
私はtask.saveを呼び出しました。それは単にスレッドに含まれるログとして動作します。エラーは表示されません。それは単に突然ロールバックします。どちらも有効ですか?何かを示しています。私はログでスレッドを更新します – Leandro