错误的参数数量(1比2),但我相信我已经过了两个

时间:2014-06-16 22:28:40

标签: ruby-on-rails

即使我传递了两个参数,我也得到wrong number of arguments (1 for 2)

我试图反正..

这是应用程序跟踪。

app/controllers/ips_dashboard_controller.rb:6:in `initialize'
app/controllers/ips_dashboard_controller.rb:82:in `new'
app/controllers/ips_dashboard_controller.rb:82:in `block (2 levels) in ips_dashboard'
app/controllers/ips_dashboard_controller.rb:81:in `each'
app/controllers/ips_dashboard_controller.rb:81:in `block in ips_dashboard'
app/controllers/ips_dashboard_controller.rb:74:in `each'
app/controllers/ips_dashboard_controller.rb:74:in `ips_dashboard'

这里我在数据库中查找ip地址并将数组传递给IP_data类以在Seer :: visualize中使用。

# Begin lookups for tgt addresses
target_ip_data = Array.new
@tgt_ip_array = Array.new
@events.each do |ip_event|
  def get_target_ip(sid,cid)
    IpsIpHdr.where('sid =? and cid =?', sid, cid).first.ip_dst
  end
  tgt_ip = get_target_ip(ip_event.sid, ip_event.cid).to_s(16).rjust(8,'0').scan(/.{2}/).map(&:hex).join('.')
  target_ip_data.push(tgt_ip)
  @tgt_ip_hash = Hash[target_ip_data.group_by {|x| x}.map {|k,v| [k,v.count]}]
  @tgt_ip_hash.each do |t|
    @tgt_ip_array.push(IP_data.new(:ip => t[0],:count => t[1]))
  end
end
# End lookups for tgt addresses

我也试过这个,但也有错误。 undefined method 'ip' for ["172.31.251.13", 24]:Array

# Begin lookups for tgt addresses
target_ip_data = Array.new
@tgt_ip_array = Array.new
@events.each do |ip_event|
  def get_target_ip(sid,cid)
    IpsIpHdr.where('sid =? and cid =?', sid, cid).first.ip_dst
  end
  tgt_ip = get_target_ip(ip_event.sid, ip_event.cid).to_s(16).rjust(8,'0').scan(/.{2}/).map(&:hex).join('.')
  target_ip_data.push(tgt_ip)
  @tgt_ip_hash = Hash[target_ip_data.group_by {|x| x}.map {|k,v| [k,v.count]}]
  @tgt_ip_hash.each do |t|
    IP_data.new(t[0],t[1])
  end
end
# End lookups for tgt addresses 

这是错误

undefined method `ip' for ["172.31.251.13", 24]:Array
Extracted source (around line #186):
183: 
184: <%=
185:     if @tgt_ip_hash.count > 0
186:       raw Seer::visualize(
187:                   @tgt_ip_hash,
188:                   :as => :pie_chart,
189:                   :in_element => 'tgt_pie_chart',

这是班级

class IP_data
  attr_accessor :ip, :count

  def initialize(ip, count)
    @ip = ip
    @count = count
  end
end

1 个答案:

答案 0 :(得分:5)

您实际上是在发送哈希:

IP_data.new({:ip => t[0],:count => t[1]})

只是做:

IP_data.new(t[0],t[1])

你仍然需要上一个循环(你在循环中删除了@ tgt_ip_arrat.push),改为:

  @tgt_ip_hash.each do |t|
    @tgt_ip_array.push(IP_data.new(t[0],t[1]))
  end
相关问题