2016-11-24 45 views
0

我有以下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

所以我用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}]}> 

回答

0

您需要登錄模型錯誤production.log。實現這一目標的方式是通過做:

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

或調試與byebug(Rails的5+)或調試器(Rails的4-)

def your_action 
    [...] 
    byebug 
    [...] 
end 

而且在軌服務器可以檢查@task變量。

+0

謝謝!我使用byebug,我發現了@ task.task_activities返回任務activitiy情況不如預期,它的活動和實例化操作的ID,但沒有任務ID笑 任何想法,爲什麼它會是這樣嗎? – Leandro

+0

你必須調用task.save,然後檢查錯誤(task.errors)。如果記錄存在驗證錯誤,則不會保存。其他方式來實現這一點是通過使用task.valid?然後task.errors。這不會嘗試將記錄保存到數據庫。 – vl3

+0

我叫task.save。它只是表現爲我在線程中包含的日誌。沒有顯示錯誤。它只是突然回滾。既沒有效?顯示任何東西我將更新線程與日誌 – Leandro

0

的解決方案是在has_many :task_activities前面添加, inverse_of: :task。 問題在於任務活動試圖在任務之前保存。我使用的是ebug(謝謝@ vl3),並檢查了@ save.errors。我找到了消息:

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

而且也沒有被TASK_ID被分配到任務活動。

我添加的這一行只是簡單地使任務在任務活動之前被創建。

相關問題