我假设教程中的第二个代码块应该在模型中,因为他后来说对控制器什么都不做。不过,示波器似乎向控制器代码发出信号。我已将以下代码添加到“扫描”模型中,该代码应该在创建扫描之前生成子“hostScan”对象
class Scan < ActiveRecord::Base
attr_accessible :description, :endTime, :startTime, :raw
has_many :hostScans, dependent: :destroy
accepts_nested_attributes_for :hostScans, :allow_destroy => true
before_create :interpret
def interpret
#parse the start and end times of the scan
self.startTime = raw.split(/(?<=timestamps\|\|\|scan_start\|)(.*?)(?=\|)/)[1]
self.endTime = raw.split(/(?<=timestamps\|\|\|scan_end\|)(.*?)(?=\|)/)[1]
#host scan bodies
#host name
#hostScans = raw.scan(/(?<=timestamps\|\|)(.*?)(?=\|host_start)/)
#self.HostScans_attributes = [{}]
#raw host text
hostScanBodies = raw.split(/(?<=host_start\|)(.*?)(?=host_end)/)
hostScanBodies.each do |body|
self.HostScans_attributes += [{:raw => body}]
end
end
end
但是,当我尝试创建扫描时,出现以下错误:
NoMethodError in ScansController#create
undefined method `HostScans_attributes' for #<Scan:0x2441e68>
它似乎不知道 HostScans_attributes。