簡體   English   中英

無法使用帶有Strong_parameters和嵌套屬性的POST更新或創建記錄

[英]Unable to update or create records using POST with strong_parameters and nested attributes

當我從表單中過帳時,我無法弄清楚為什么我的數據庫記錄沒有得到更新或創建了新記錄。

我能夠手動填充數據庫並創建關系:

contact = Contact.first
command = Command.find(3)
contact.host_notification_commands << command

而且我還可以在我的表單中加載此數據信息。 我不知道如何更新或添加新記錄。 到目前為止,我嘗試過的一切都失敗了。

我有3個模型CommandContact和Joined CommandsContact commands_contacts聯接表具有一個額外的屬性:notification_type ,可以將其設置為主機服務 ,並且我的Contact模型設置了2個額外的關系,我可以訪問:host_notification_commands:service_notification_commands 這使我能夠執行諸如Contact.fist.host_notification_commandsContact.find(3).service_notification_commands.

似乎沒有UPDATEINSERT查詢從控制器觸發,當我執行正確的POST時,我不知道如何調試它。

楷模

class Command < ActiveRecord::Base

  has_many :commands_contacts
  has_many :contacts, :through => :commands_contacts

end

class Contact < ActiveRecord::Base

    has_many :commands_contacts
    has_many :commands, :through => :commands_contacts

    has_many :host_notification_commands, -> { where commands_contacts: { :notification_type => 'host' } },
            :through => :commands_contacts,
            :class_name => 'Command', 
            :source => :command

    has_many :service_notification_commands, -> { where commands_contacts: { notification_type: 'service' } },
            :through => :commands_contacts,
            :class_name => 'Command', 
            :source => :command

    accepts_nested_attributes_for :commands, :host_notification_commands, :service_notification_commands

end

class CommandsContact < ActiveRecord::Base

    belongs_to :command
    belongs_to :contact

    accepts_nested_attributes_for :command

end

在此之后,一切都崩潰了。

控制者

由於我正在使用accepts_nested_attributes_for ,因此我必須將_attributes附加到嵌套對象的名稱- :host_notification_commands:service_notification_commands 我將更改表單以這種方式提交,但是出於示例目的,簡單的重新分配有效。

def update
    contact = Contact.find_by_id(params[:id])
    contact.update(safe_params)
end    

private
def safe_params

    params[:contact][:host_notification_commands_attributes] = params[:contact][:host_notification_commands]
    params[:contact][:service_notification_commands_attributes] = params[:contact][:service_notification_commands]

    params.require(:contact)
        .permit(:contact_name, :host_notification_commands_attributes => [:id, :command_name, :command_line, :command_description],
                :service_notification_commands_attributes => [:id, :command_name, :command_line, :command_description])
end

更新現有記錄結果:

#<ActiveRecord::RecordNotFound: Couldn't find Command with ID=2 for Contact with ID=1>

當然不存在! 我正在嘗試建立這種關系!

添加一個新的,我得到:

#<ActiveRecord::RecordNotFound: Couldn't find Command with ID=1 for Contact with ID=>

完全正確。 用戶甚至還沒有創建,並且還沒有通過命令建立關系,為什么Rails試圖找到它?

我也沒有在Rails控制台中看到任何更新或插入查詢,因此我想它甚至沒有達到這一點...

D, [2015-01-20T18:01:30.336669 #95542] DEBUG -- :    (0.1ms)  BEGIN
D, [2015-01-20T18:01:30.338971 #95542] DEBUG -- :   Command Load (0.3ms)  SELECT `commands`.* FROM `commands` INNER JOIN `commands_contacts` ON `commands`.`id` = `commands_contacts`.`command_id` WHERE `commands_contacts`.`contact_id` = 1 AND `commands_contacts`.`notification_type` = 'host' AND `commands`.`id` IN (1, 3)
D, [2015-01-20T18:01:30.340555 #95542] DEBUG -- :   Command Load (0.2ms)  SELECT `commands`.* FROM `commands` INNER JOIN `commands_contacts` ON `commands`.`id` = `commands_contacts`.`command_id` WHERE `commands_contacts`.`contact_id` = 1 AND `commands_contacts`.`notification_type` = 'service' AND `commands`.`id` IN (4, 2)
D, [2015-01-20T18:01:30.341501 #95542] DEBUG -- :    (0.1ms)  ROLLBACK
#<ActiveRecord::RecordNotFound: Couldn't find Command with ID=2 for Contact with ID=1>
Completed 200 OK in 11ms (Views: 0.4ms | ActiveRecord: 0.9ms)

我在這里想念什么?

編輯:我想我可以放棄使用strong_parameters的想法,解析所有POST參數,然后手動填充數據庫,但是那不是Rails-y。

編輯2:包括發布到控制器的參數。

數據以params[:contact]形式輸入

{
                     "contact_name" => "joe-user",
       "host_notification_commands" => [
        [0] {
                             "id" => 1,
                   "command_name" => "host-notify-by-email",
                   "command_line" => "/usr/local/bin/host-notify",
            "command_description" => "Host Alert",
                     "created_at" => "2015-01-19T17:24:12.000Z",
                     "updated_at" => "2015-01-21T03:29:03.000Z"
        },
        [1] {
                             "id" => 2,
                   "command_name" => "host-notify-by-pager",
                   "command_line" => "/usr/local/bin/host-notify-pager",
            "command_description" => "Host Alert by Pager",
                     "created_at" => "2015-01-19T17:24:33.000Z",
                     "updated_at" => "2015-01-19T17:24:33.000Z"
        }
    ],
    "service_notification_commands" => [
        [0] {
                             "id" => 4,
                   "command_name" => "service-notify-by-email",
                   "command_line" => "/usr/local/bin/service-notify",
            "command_description" => "Service Alert",
                     "created_at" => "2015-01-19T17:24:44.000Z",
                     "updated_at" => "2015-01-19T17:24:44.000Z"
        }
    ]
}

經過strong_parameters之后,它變成了:

本質上是一樣的,只是剝離了created_atupdated_at並將_attributes附加到屬性名稱之后,因此它與accept_nested_attributes_for一起accept_nested_attributes_for

{
                                "contact_name" => "joe-user",
       "host_notification_commands_attributes" => [
        [0] {
                             "id" => 1,
                   "command_name" => "host-notify-by-email",
                   "command_line" => "/usr/local/bin/host-notify",
            "command_description" => "Host Alert"
        },
        [1] {
                             "id" => 2,
                   "command_name" => "host-notify-by-pager",
                   "command_line" => "/usr/local/bin/host-notify-pager",
            "command_description" => "Host Alert by Pager"
        }
    ],
    "service_notification_commands_attributes" => [
        [0] {
                             "id" => 4,
                   "command_name" => "service-notify-by-email",
                   "command_line" => "/usr/local/bin/service-notify",
            "command_description" => "Service Alert"
        }
    ]
}

編輯#3:我已啟用MySQL查詢日志記錄,但我根本看不到執行UPDATE / INSERT查詢。 我如何調試為什么contact.update(safe_params)沒有做任何事情?

編輯#4:作為沒有AngularJS或POST的簡單測試,我做了一個簡單的rake任務,該任務定義了JSON對象並嘗試更新db。 我遇到了同樣的問題,因此我非常確信該問題在我的模型中某處...但是在哪里?

請看看這個要點https://gist.github.com/pruchai/6afe74b170da2a3d307f

構建表單並使用nested_fields_for表單結構時,構建正確。 因此,理想情況下,您將首先嘗試以“老式方式”執行此操作,然后檢查推送到服務器的內容並進行模擬。

因此,通常我不會手動執行此操作,但我會維護一個gem來處理嵌套表單(繭),因此,如果我沒有記錯的話, accepts_nested_attributes起作用,參數必須如下所示:

{
    "contact_name" => "joe-user",
    "host_notification_commands_attributes" => {
      "0" => {
        "id" => 1,
        "command_name" => "host-notify-by-email",
        "command_line" => "/usr/local/bin/host-notify",
        "command_description" => "Host Alert"
      },
      "1" => {
        "id" => 2,
        "command_name" => "host-notify-by-pager",
        "command_line" => "/usr/local/bin/host-notify-pager",
        "command_description" => "Host Alert by Pager"
      }
    },
    "service_notification_commands_attributes" => {
      "0" => {
        "id" => 4,
        "command_name" => "service-notify-by-email",
        "command_line" => "/usr/local/bin/service-notify",
        "command_description" => "Service Alert"
      }, 
      "12342233444444" => {
        "command_name" => "service-notify-by-email",
        "command_line" => "/usr/local/bin/service-notify",
        "command_description" => "Service Alert"
      }
    }
  }

因此, .._attributes不應包含數組,而應包含哈希。 關鍵在於(現有)數組中的位置,我認為。 在繭中,我用從當前時間得出的一些大數字填充此虛擬索引 我假設此順序應與該關系的默認順序相對應。 但是,如果您將“ id”字段交給您,那么Rails也可能會使用它。 不確定。 要銷毀現有元素,您必須設置_destroy屬性(並在強參數中也允許它)。

恕我直言,新項目的id應該為空,否則Rails會假定該項目存在(因為通常不會在客戶端生成ID)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM