我一直盯着这段代码太长,直到3天前,代码工作正常,所以我希望一些新鲜的眼睛会有所帮助。
我的视图应该为一个位置呈现网络主机。然后它将主机拆分为两个选项卡:安全百分比小于99%的主机,以及安全百分比为99%或更高的主机。我的观点只是渲染安全百分比为99%或更高的主机。
network_hosts_controller.rb
class V1::NetworkHostsController < ApplicationController
before_action :set_location
before_action :set_network_host, only: [:show]
def index
@network_hosts = NetworkHost.all
end
def show
end
private
def set_location
@location = Location.find_by!(identifier: params[:location_id])
end
def set_network_host
@network_host = NetworkHost.find(params[:id])
end
end
&#13;
模型/ network_host.rb
class NetworkHost < ActiveRecord::Base
acts_as_paranoid
belongs_to :location
has_many :network_host_tests, dependent: :destroy
has_many :parent_tests, through: :network_host_tests
validates_presence_of :location, :mac_address, :ip_address
validates_uniqueness_of :mac_address
delegate :security_percentage, to: :last_test, allow_nil: true
def name
if self.hostname.blank?
self.ip_address
else
"#{self.ip_address} (#{self.hostname})"
end
end
def last_test
network_host_tests.last
end
def test_before_last_test
network_host_tests.offset(1).last
end
end
&#13;
视图/ network_hosts / _index.html.erb
<section id="network-hosts" class="list-outer">
<header class="section-header hosts-header clearfix">
<h3>Network Hosts by Risk</h3>
</header>
<ul class="nav nav-tabs" id="myTab">
<li class="active"><a data-toggle="tab" onclick="$('#hosts-list-issues').show();$('#hosts-list-scanned').hide();" >Hosts at Risk</a></li>
<li><a data-toggle="tab" onclick="$('#hosts-list-issues').hide();$('#hosts-list-scanned').show();">Scanned Hosts</a></li>
</ul>
<% network_hosts = network_hosts.sort_by{|e| e.security_percentage} %>
<div id="hosts-list-issues" class="hosts dl-menuwrapper">
<ul class="dl-menu">
<% network_hosts.each do |network_host| %>
<% if network_host.security_percentage < 99 %>
<% next unless last_test = network_host.last_test %>
<li class="clearfix">
<a href="#" class="last-test-notif <%= last_test.security_percentage_movement_since_last_test %>">
<div class="tooltip-content" data-status="<%= last_test.security_percentage_movement_since_last_test %>">
<p class="last-test-label">Last Test: </p>
<% if last_test %>
<span class="last-test-date"><%= last_test.date.to_s(:humanized_ago) %></span>
<% else %>
-
<% end %>
</div>
</a>
<a href="#">
<div class="host-graph-wrapper">
<div class="host-chart" data-percent="<%= network_host.security_percentage %>">
<span class="host-percent"></span>
</div>
</div>
<div class="host-data">
<p><span class="host-address"><%= network_host.name %></span></p>
<p><span class="num-issues">
<% if last_test.results.count > 0 %>
<%= last_test.results.count %></span> Issues Detected: <span class="severity"><%= last_test.results.count %> Major</span>, <span class="severity"><%= last_test.results.count %> Minor
<% else %>
No Issues Detected
<% end %>
</span></p>
</div>
</a>
<ul class="dl-submenu">
<li class="overview">
<a href="#" class="last-test-notif <%= last_test.security_percentage_movement_since_last_test %>">
<div class="tooltip-content" data-status="<%= last_test.security_percentage_movement_since_last_test %>">
<p class="last-test-label">Last Test: </p>
<% if last_test %>
<span class="last-test-date"><%= last_test.date.to_s(:humanized_ago) %></span>
<% else %>
-
<% end %>
</div>
</a>
<div class="host-graph-wrapper">
<div class="host-chart" data-percent="<%= network_host.security_percentage %>">
<span class="host-percent"></span>
</div>
</div>
<div class="host-data">
<p><span class="host-address"><%= network_host.name %></span></p>
<p>
<span class="host-mac-address">
<a href="http://www.coffer.com/mac_find/?string=<%= network_host.mac_address %>" target="_blank" onclick="return confirm('You are about to be redirect to an external website.')"><%= network_host.mac_address %></a>
</span>
</p>
<p><span class="num-issues">
<% if last_test.results.count > 0 %>
<%= last_test.results.count %></span> Issues Detected: <span class="severity"><%= last_test.issues.where(:severity => "major").count %> Major</span>, <span class="severity"><%= last_test.issues.where(:severity => "minor").count %> Minor
<% else %>
No Issues Detected
<% end %>
</span></p>
</div>
</li>
<% last_test.results.each do |result| %>
<% issue = result.issue %>
<li class="port-list">
<a href="#">
<span class="status-dot <%= issue.minor? ? 'yellow' : 'red' %>"></span>
<span class="port-list-port"><%= issue.name %></span>
</a>
<ul class="dl-submenu description">
<li class="content">
<h5><%= issue.name %></h5>
<h6>Description</h6>
<p><%= issue.description || "-" %></p>
<h6>Problem</h6>
<p><%= issue.problem || "-" %></p>
<h6>The Fix</h6>
<p><%= issue.solution || "-" %></p>
</li>
</ul>
<!-- End issue details -->
</li>
<!-- End issue content -->
<% end %>
<!-- End results -->
</ul>
<!-- End submenu -->
</li>
<!-- End clearfix -->
<% end %>
<% end %>
<!-- End network_hosts -->
</ul>
<!-- End ul -->
</div>
<!-- End hosts -->
<div id="hosts-list-scanned" class="hosts dl-menuwrapper" style="display:none;">
<ul class="dl-menu">
<% network_hosts.each do |network_host| %>
<% if network_host.security_percentage >= 99 %>
<% next unless last_test = network_host.last_test %>
<li class="clearfix">
<a href="#" class="last-test-notif <%= last_test.security_percentage_movement_since_last_test %>">
<div class="tooltip-content" data-status="<%= last_test.security_percentage_movement_since_last_test %>">
<p class="last-test-label">Last Test: </p>
<% if last_test %>
<span class="last-test-date"><%= last_test.date.to_s(:humanized_ago) %></span>
<% else %>
-
<% end %>
</div>
</a>
<a href="#">
<div class="host-graph-wrapper">
<div class="host-chart" data-percent="<%= network_host.security_percentage %>">
<span class="host-percent"></span>
</div>
</div>
<div class="host-data">
<p><span class="host-address"><%= network_host.name %></span></p>
<p><span class="num-issues">
<% if last_test.results.count > 0 %>
<%= last_test.results.count %></span> Issues Detected: <span class="severity"><%= last_test.issues.where(:severity => "major").count %> Major</span>, <span class="severity"><%= last_test.issues.where(:severity => "minor").count %> Minor
<% else %>
No Issues Detected
<% end %>
</span></p>
</div>
</a>
<ul class="dl-submenu">
<li class="overview">
<a href="#" class="last-test-notif <%= last_test.security_percentage_movement_since_last_test %>">
<div class="tooltip-content" data-status="<%= last_test.security_percentage_movement_since_last_test %>">
<p class="last-test-label">Last Test: </p>
<% if last_test %>
<span class="last-test-date"><%= last_test.date.to_s(:humanized_ago) %></span>
<% else %>
-
<% end %>
</div>
</a>
<div class="host-graph-wrapper">
<div class="host-chart" data-percent="<%= network_host.security_percentage %>">
<span class="host-percent"></span>
</div>
</div>
<div class="host-data">
<p><span class="host-address"><%= network_host.name %></span></p>
<p>
<span class="host-mac-address">
<a href="http://www.coffer.com/mac_find/?string=<%= network_host.mac_address %>" target="_blank" onclick="return confirm('You are about to be redirect to an external website.')"><%= network_host.mac_address %></a>
</span>
</p>
<p><span class="num-issues">
<% if last_test.results.count > 0 %>
<%= last_test.results.count %></span> Issues Detected: <span class="severity"><%= last_test.issues.where(:severity => "major").count %> Major</span>, <span class="severity"><%= last_test.issues.where(:severity => "minor").count %> Minor
<% else %>
No Issues Detected
<% end %>
</span></p>
</div>
</li>
<% last_test.results.each do |result| %>
<% issue = result.issue %>
<li class="port-list">
<a href="#">
<span class="status-dot <%= issue.minor? ? 'yellow' : 'red' %>"></span>
<span class="port-list-port"><%= issue.name %></span>
</a>
<ul class="dl-submenu description">
<li class="content">
<h5><%= issue.name %></h5>
<h6>Description</h6>
<p><%= issue.description || "-" %></p>
<h6>Problem</h6>
<p><%= issue.problem || "-" %></p>
<h6>The Fix</h6>
<p><%= issue.solution || "-" %></p>
</li>
</ul>
<!-- End issue details -->
</li>
<!-- End issue content -->
<% end %>
<!-- End results -->
</ul>
<!-- End submenu -->
</li>
<!-- End clearfix -->
<% end %>
<% end %>
<!-- End network_hosts -->
</ul>
<!-- End ul -->
</div>
<!-- End hosts -->
</section>
&#13;
应该呈现安全百分比低于99%的主机的区域是host-list-issues
div id,特别是host-data
div类,但它现在不是:
真正让我失望的是,在使用不同主机的不同登录下,它确实呈现:
我已经通过Rails控制台运行代码,以确保它实际上也返回了内容:
network_hosts = NetworkHost.all
network_hosts = network_hosts.sort_by{ |e| e.security_percentage }
network_hosts.each do |network_host|
if network_host.security_percentage < 99
next unless last_test = network_host.last_test
end
end
end
&#13;
哪些会返回对象(代码段):
#<NetworkHost id: 32, location_id: 8, mac_address: "84:38:35:AA:4D:3D", ip_address: "192.168.1.100", hostname: "A Phone", created_at: "2015-03-05 18:00:18", updated_at: "2015-06-06 14:47:04", deleted_at: nil, vendor: nil>,
#<NetworkHost id: 56, location_id: 8, mac_address: "28:0D:FC:E3:5C:43", ip_address: "192.168.1.129", hostname: nil, created_at: "2015-05-21 16:03:19", updated_at: "2015-05-21 16:03:19", deleted_at: nil, vendor: nil>,
#<NetworkHost id: 52, location_id: 10, mac_address: "64:76:BA:9D:11:87", ip_address: "10.10.10.102", hostname: "Razor", created_at: "2015-04-09 15:02:41", updated_at: "2015-06-07 14:55:45", deleted_at: nil, vendor: nil>,
#<NetworkHost id: 41, location_id: 8, mac_address: "54:9F:13:2A:28:16", ip_address: "192.168.1.132", hostname: "Another Phone", created_at: "2015-03-05 18:00:18", updated_at: "2015-06-05 15:29:33", deleted_at: nil, vendor: nil>,
#<NetworkHost id: 44, location_id: 8, mac_address: "18:20:32:C1:32:D8", ip_address: "192.168.1.138", hostname: "Thermostat", created_at: "2015-03-05 20:00:27", updated_at: "2015-06-20 15:02:55", deleted_at: nil, vendor: nil>,
#<NetworkHost id: 58, location_id: 3, mac_address: "B8:27:EB:C1:43:42", ip_address: "10.10.10.133", hostname: nil, created_at: "2015-06-02 00:12:19", updated_at: "2015-06-02 00:12:19", deleted_at: nil, vendor: nil>
&#13;
因此,在主机出现在某些位置而非其他位置之间,以及当我直接从Rails控制台拉出结果时,我很难过。救命啊!