1
votes

I have a controller with an API request showing all my Google Docs.

feed = client.get('http://docs.google.com/feeds/documents/private/full').to_xml

    feed.elements.each('entry') do |entry|
      puts 'title: ' + entry.elements['title'].text
      puts 'type: ' + entry.elements['category'].attribute('label').value
      puts 'updated: ' + entry.elements['updated'].text
      puts 'id: ' + entry.elements['id'].text

      # Extract the href value from each <atom:link>
      links = {}
      entry.elements.each('link') do |link|
        links[link.attribute('rel').value] = link.attribute('href').value
      end
      puts links.to_s

end

So, I can see the results in my console but how do I get them into my view?

I tried with something like this, but that doesn't work (I changed my variable in the controller to an accessor of course)

<% feed.elements.each('entry') do |entry| %> <% entry.elements['title'].text %> <% end %>

2

2 Answers

0
votes

First, in your controller, make feed an instance variable. IE: it should be:

@feed = client.get..... instead of feed = client.get....

If that doesn't fix it... I don't know your API for sure, but I suspect you may need to be using:

<% @feed.elements.each('entry') do |entry| %> <% entry['title'] %> <% end %>

Note: entry['title'] instead of entry.elements['title'].text

What your current code indicates is that the feed is structured like this:

feed.elements[0].elements['attr'].text, when it's probably just feed.elements[0]['attr']

Does that make sense? Try that and see what happens.

If that doesn't work, just put: debug(@feed) in your view and copy and paste it to the end of your question. That'll help us figure out the right way to access this info.

0
votes

Problem solved. Because I use 'puts' in the controller to show the content of the feed in the console I also have to change that for the view. Of course, puts is equal to <%= ... %>.

<ul>
    <% @feed.elements.each('entry') do |entry| %>
        <li><%= 'title: ' + entry.elements['title'].text %></li>
    <% end %>
</ul>