Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Passing xml values to array then to a view

nokogiri xpath rails ruby

  • Please log in to reply
No replies to this topic

#1 haribo2014

haribo2014

    Passenger

  • Members
  • 2 posts

Posted 17 May 2014 - 05:17 PM

Hi,

 

I'm bringing data into my Rails application using xpath in my application controller. I'm trying to put the data into an array and then pass into the application.html.erb which stores a highchart. I jsut cant get the graph to populate with the values, can anyone provide any help?

class ApplicationController < ActionController::Base
 
require 'nokogiri'
require 'open-uri'
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery
  before_filter :set_locale
def index
  
@doc = Nokogiri::XML(open("http://api.worldbank.org/countries/BRA/indicators/1.0.HCount.2.5usd?per_page=10&date=1960:2014"))
@values = Array.new(9)
for i in 0 .. 8
  @values[i] = doc.xpath('//wb:value')
end
end

  
  
  
  private
  
  
  def authorize
  redirect_to login_url, alert: "Not authorized" if current_user.nil?
end
  
  
  
  
def current_user
  @current_user ||= User.find_by_auth_token!(cookies[:auth_token]) if cookies[:auth_token]
end
helper_method :current_user
  

  
  def set_locale
    I18n.locale = params[:locale] if params[:locale].present?
    
  end
  
def default_url_options(options = {})
  {locale: I18n.locale}
end  
  
  

  
end
<!DOCTYPE html>
<html>
<head>
  <title>Project</title>
  <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %>
  <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
   <%= javascript_include_tag :defaults %>
  <%= csrf_meta_tags %>
  <script src="http://code.highcharts.com/highcharts.js"></script>
<script src="http://code.highcharts.com/modules/exporting.js"></script>
<script>  
$(function () {
        $('#container').highcharts({
            chart: {
                type: 'column'
            },
            title: {
                text: 'Brazil Poverty Headcount'
            },
            subtitle: {
                text: 'Source: WorldBank.com'
            },
            xAxis: {
                categories: [
                    '1998',
                    '1999',
                    '2000',
                    '2001',
                    '2002',
                    '2003',
                    '2004',
                    '2005',
                    '2006',
                    '2007'    
                ]
            },
            yAxis: {
                min: 0,
                title: {
                    text: 'X-Axis Data'
                }
            },
            tooltip: {
                headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
                pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' +
                    '<td style="padding:0"><b>{point.y:.1f} mm</b></td></tr>',
                footerFormat: '</table>',
                shared: true,
                useHTML: true
            },
            plotOptions: {
                column: {
                    pointPadding: 0.2,
                    borderWidth: 0
                }
            },
            series: [{
                name: 'Brazil',
                data: [<%=@values[0]%>, <%=@values[1]%>, <%=@values[2]%>, <%=@values[3]%>, <%=@values[4]%>, <%=@values[5]%>, <%=@values[6]%>, <%=@values[7]%>, <%=@values[8]%>]
            }]
        });
    });  
   
  
</script>
  
  
</head>
<body>
  
 <div id="container" style="min-width: 310px; height: 600px; margin: 15 auto"></div> 

<div id ="signing" style="width:200px; height:200px; float:right;">
<% if current_user %>
  Logged in as <%= current_user.email %>
  <%= link_to "Log Out", logout_path %>
  <% else %>
<%= link_to "Sign Up", signup_path %>
<%= link_to "Log In", login_path %>
<% end %>
  
</div>

<div id="admin" style="width:100px; height:100px; float:right;">
<%= link_to "Admin", new_admin_user_session_path  %>
</div>


<div>
Language:
<%= link_to_unless_current "English", locale: "en" %> |
<%= link_to_unless_current "Spanish", locale: "sp" %>


</div>


<%= yield %>


</body>
</html>







Also tagged with one or more of these keywords: nokogiri, xpath, rails, ruby

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users