Problem with displaying my XML response from API in view

Hi I cant seem to figure out this problem. Im using Nokogiri to connect to an API and parse the XML response. I am then trying to display it in my view. I can se the data in my Terminal when I do some debugging on the XML object but cant seem to get it to the view. I have pasted some of the code here http://refactormycode.com/codes/1567-problem-with-passing-xml-from-api-to-view Please all help is appreciated. Thanks!!

So, what happens when you try and access view this page in your browser ? have you verified what @news contains ?

Fred

Hi Fred! I did some output on the @news object in my view and I do get some console output of the xml response but in the browser its empty. How do you do some descent debugging on objects in Rails 3?! First I did some good old puts and then I merely did <%= @news %>. So to round it up. It seems though as @news contains the xml but I cant display it in the browser. Also I cant see the <li> for some unexplained reason. When I debug in the browser the DOM stops at <ul id="news"> Feeling a bit confused about this:)

You can use ruby-debug to break into the code (model, controller or view) in order to inspect data. See the Rails Guide on debugging for how to do this, and other tips for debugging.

Colin

Has anyone else any feedback to give me on this?! I still havent progressed in the matter. Really frustrating. Thanks in advance!! Regards

Have you tried doing:

raise @news.to_yaml

In the controller and seeing that that brings?

Hmm I did as you said John but Im just getting an undefined method `to_yaml=' for nil:NilClass. Is my object empty?! is that what happens?? Thanks?!

Has anyone else any feedback to give me on this?! I still havent progressed in the matter. Really frustrating. Thanks in advance!!

So what did you learn from debugging it?

Colin

So what did you learn from debugging it?

Colin

Well Colin Im a bit confused. I had some problems with installing the ruby-debug as Im using Rails 3. So finally I gave up with that. But when I did puts doc.to_s I get the xml in my console so I have it there. But the line after where I do this: @news = doc.css('news').map do |n| I tried to do @news.to_s and that gave me nothing. Also I did raise @news.to_yaml which just game me an error. Do u have any clues on that?!

Regards!

So what did you learn from debugging it?

Colin

Well Colin Im a bit confused. I had some problems with installing the ruby-debug as Im using Rails 3. So finally I gave up with that.

Persevere. Rails 3 should be no problem. Did you follow the guide here: http://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-ruby-debug If so what problem did you have? You need to be able to break into code and inspect data and run code snippets to see what is wrong. Even if you sort out your current issue there will be another that needs the debugger.

Colin

Colin! I did this in debugger mode. display @stories 1: @stories = It seems as though he object is empty? As i suspected. I cant understand how it can be when I am declaring it with the doc container which should contain the api url?? Very confusing this. Im trying to invoke the debugger around this peace of code but without success debugger     @stories = doc.css(".item").map do |n| Here I cant even get in to debug mode. Grateful for all your help.

Thanks!

I finally managed to debug more around the @stories variable. This is an outtake from it (rdb:14) display @stories 1: @stories = (rdb:14) show @stories Unknown show command @stories (rdb:14) p @stories nil (rdb:14) pp stories NameError Exception: undefined local variable or method `stories' for #<NewsController:0x102ece778> (rdb:14) So my conclusion is from this segment of code: apiKey = ("http:// www.mysomething.com/partner/api/1_0/randomnumber/channel/290/material/list/")     doc = Nokogiri::XML(open(apiKey).read) <-- here I have the xml response and all is dandy but the next line I do this: @stories = doc.css(".item").map do |n| <--here its empty.       h = {}       %w(header source_name summary url geo created_at type_of_media).each do |key|         h[key.to_sym] = n.css(key).text       end       News.new(h)     end

How is this possible? I might be new to ruby and rails but Im feeling really lost. Any good feedback?! Thanks!

How is this possible? I might be new to ruby and rails but Im feeling really lost. Any good feedback?! Thanks!

Are you inspecting the variable before or after it has been set? Have you tried refactoring the bit which fetches and parses the xml feed into its own class so that you can more easily play with it in the console (and test it) ?

Fred

Hi Fred! The bit wich fetches the xml and parses it I have done a regular puts on that many times and i did 'var local' in debugger mode which displayed all local variables and then I got all the xml output. Before I did the debugging actually before it has been set. Im trying to do it after now as well but for some reason it has problems going in to debug mode. I do Rails s --debugger and it says that "Debugger enabled" but it doesnt get there. Do you have any feedback from the link a pasted with code on refactormycode.com? Is it something obviously wrong Im doing when declaring the variabe? Thanks in advance!