2011-09-16 2 views
0

here (GitHub 저장소 here) 코드를 사용하여 히트 맵/클라우드를 생성하고 있습니다.Ruby/Rails : Bantik의 히트 맵 코드가 작동하지 않습니다.

많은 NoMethodErrors 및 페이지 렌더링이 방해되는 다른 문제가 발생합니다. 페이지 의 2 ~ 3 번이으로 렌더링되면 클라우드가 제대로 표시되지 않습니다. 이 코드를 가지고 놀 수 있도록 모든 노력을 기울여 주신 것에 대해 감사드립니다.

시간의 오류는 다음과 같습니다

: 나는을 받고 있었기 때문에 내가보기에 직접 테스트 해시를 덤핑하고있어

<h1>Title</h1> 
. 
. 
. 
<% stylesheet_link_tag "custom" %> 
<%= heatmap({"Foo" => 13, 
    "Bar" => 15, 
    "Trouble" => 5, 
    "Braids" => 1, 
    "Something" => 9, 
    "Else" => 13, 
    "Many" => 20,   <---- 
    "Zombies" => 7, 
    "nothing" => 0}) %> 

:

undefined method `each_pair' for "Many":String 

이 내 시야를 참조하고

undefined method `keys' for nil:NilClass 

완성을 위해, 이것은 내 ApplicationHelper (히트 맵 방법이 정의 된 곳)입니다 :

module ApplicationHelper 
    def heatmap(histogram={}) 

    content_tag(:div, :class => "heatmap") 

    histogram.keys.sort{|a,b| histogram[a] <=> histogram[b]}.reverse.each do |k| 
     next if histogram[k] < 1 
     _max = histogram_max(histogram) * 2 
     _size = element_size(histogram, k) 
     _heat = element_heat(histogram[k], _max) 

     content_tag(:span, content_tag(:class => "heatmap_element", :style => "color: ##{_heat}#{_heat}#{_heat}; font-size: #{_size}px;"), "#{k}") 
    end 

    content_tag(:br, :style => "clear: both;") 
    end 

    def histogram_max(histogram) 
    histogram.map{|k,v| histogram[k]}.max 
    end 

    def element_size(histogram, key) 
    (((histogram[key]/histogram.map{|k,v| histogram[k]}.sum.to_f) * 100) + 5).to_i 
    end 

    def element_heat(val, max) 
    sprintf("%02x" % (200 - ((200.0/max) * val))) 
    end 
end 

나는 저장소에서 코드를 가져 와서 도우미에서 대신 사용했습니다. 앱이 HTML 코드를 이스케이프 처리하고 사이트에 인쇄했기 때문에 content_tags를 사용하도록 코드를 변경했습니다.

답변

관련 문제