ruby-on-railsruby-on-rails-3open-flash-chart

Open Flash Chart 2 - the ids are the same so multiples are not displaying


I can get one successful Open Flash Chart per view. I'm now trying to support multiple Open Flash Charts per view but the source code shows that the ids for both generated divs are the same:

# javascript at top of view
<%= javascript_include_tag "swfobject.js", :cache => true %>

# view itself
<%= @graph.html_safe %>  # html_safe is necessary for Rails 3
<%= @graph2.html_safe %>

# view's source code
<div id='flashcontent_913000'></div> 
  <script type="text/javascript"> 
    swfobject.embedSWF(
    "/open-flash-chart.swf","flashcontent_913000",
    "440", "310", "9.0.0", "expressInstall.swf",
    {"data-file":"/analytics%2Fpie1"}, {}, {} );
  </script> 
</div> 
<br /> 

<div id='flashcontent_913000'></div> 
  <script type="text/javascript"> 
    swfobject.embedSWF(
    "/open-flash-chart.swf","flashcontent_913000",
    "440", "310", "9.0.0", "expressInstall.swf",
    {"data-file":"/analytics%2Fpie2"}, {}, {} );
  </script> 
</div>

#controller
respond_to do |wants|
  wants.html {
    @graph = ofc2( 440, 310, 'analytics/pie1' )
    @graph2 = ofc2( 440, 310, 'analytics/pie2' )
  }
end
def pie1
... # functioning when put in the view alone
end
def pie2
... # functioning when put in the view alone
end

Because the ids are the same, <%= @graph.html_safe %> tries to render graph2 and fails, and graph2 doesn't display at all. I need to get OFC2 to generate different ids. How can I accomplish this?


Solution

  • It turns out that you can specify an id in your controller right after 'analytics/pie1', for example. By specifying the ID specifically, we eliminate any chance of an algorithm generating the same number and therefore failing to show the images.

    Example: @graph = ofc2( 440, 310, 'analytics/pie1', '/', 'my_unique_div' )