1

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 Answers 2

0

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.

1
  • Thanks for your answer. I got some more knowledge about accessors an Ruby syntax with your tips and solved my problem (take a look at my answer). Feb 13, 2011 at 22:26
0

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>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.