I 대다 관계를 통해 통신 다음 레일 모델이 있습니다레일 모델은 저장에 실패하지만 오류 메시지가 표시됩니다
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)
내가 아무 문제없이 저장 형태로 중첩 된 속성을 가진 라인을 주석 처리합니다. 그래서 문제는 확실히 그것에 있습니다. 당신은 모델 오류를 기록 할
@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가 없습니다. lol 왜 이런 일이 벌어지고 있는지 알 수 있습니까? – Leandro
task.save를 호출 한 다음 오류 (task.errors)를 확인해야합니다. 레코드에 유효성 검사 오류가 있으면 저장되지 않습니다. 이를 달성하는 다른 방법은 task.valid를 사용하는 것입니다. 그런 다음 task.errors. 이렇게하면 레코드를 데이터베이스에 저장하지 않습니다. – vl3
나는 task.save를 호출했다. 단순히 스레드에 포함 된 로그로 작동합니다. 오류는 표시되지 않습니다. 갑작스럽게 롤백됩니다. 어느 쪽도 유효하지 않습니까?아무것도 보여줍니다. 스레드로 스레드를 업데이트합니다. – Leandro