简体   繁体   中英

Preparing XML Data for Chart Library

I am using a flash chart library that takes an xml file/string as input. The xml file is generated using a xml.builder file, but I am not sure the generated xml file is actually being found.

If there is a (name).xml.builder file and a (name).html.erb file, does that mean both an html/xml file is generated, when format.html and format.xml is called? What could be the other problem?

The relevant code snippets are below: (input_by_month.html.erb)

<script type="text/javascript" language="javascript" src="/javascripts/AnyChart.js"></script> 
</head>
<body> 
    <h2>Input by Month</h2>
    <script type="text/javascript" language="javascript"> 
    //<![CDATA[
    var chart = new AnyChart('/AnyChart.swf'); 
    chart.setXMLFile('input_by_month.xml'); 
    chart.write(); 
    //]]> 
    </script> 
 </body> 

The above javascript expects an XML file named "input_by_month.xml". I THINK the file is being built via "input_by_month.xml.builder". XML is well formed and does not contain any errors.

xml.anychart
  xml.settings do
  ....
  end
end 

The html view is generated in a controller with action named "input_by_month".

class ChartsController < ApplicationController
  def input_by_month
    @months = params[:months]
    @input_by_month = InputByMonth.find(:all, :conditions => "user_id = '#{current_user.id}' AND end_time between '#{@months.months.ago.to_date}' AND '#{Time.now.to_date}'")

    respond_to do |format|
      format.html
      format.xml
    end
  end
end

The error I was making was due to routes.rb not taking into account .xml paths. Since input_by_month is a custom action, I mapped it to routes.rb using:

map.connect "charts/input_by_month", :controller => 'charts', :action => 'input_by_month'

The above line only maps .html files. What I needed was to also add routes for .xml files. To do this I need to add an additional line:

map.connect "charts/input_by_month.:format", :controller => 'charts', :action => 'input_by_month'

Adding this line, allowed the javascript in my .html view to find the .xml file generated by the input_by_month action.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM