Question

I trying to load feeds from my blog but this is resulting in the error in the title mentioned.

The error message:

NoMethodError (undefined method `entries' for 0:Fixnum):
app/controllers/pages_controller.rb:6:in `home'

This is how I'm doing:

I created a file in the libfolder called blog_feeds.rb, that contains only the following:

module BlogFeeds
require 'feedzirra'

def load_feeds
  feeds = Feedzirra::Feed.fetch_and_parse('http://blog.shigotodoko.com/feed')
end

end

And the line #6 for the error is:

@feeds = load_feeds.entries

Note that this error only occurs sometimes, not always.

So, any idea about what's going wrong here?

Thank you!

Was it helpful?

Solution 2

Well, seems that was something wrong with my code before.

I was trying to randomize some posts and using something like this on the view:

@feeds.shuffle!.first(5)

In order to get the first 5 random posts.

And to fix it, I just replaced the shuffle! method for the shufflemethod.

Now, everything is working fine!

OTHER TIPS

When fetching a feed, Feedzirra will return the HTTP status code instead of an object containing the feed entries, if the HTTP fetch results in an error (i.e. not a 200 or 3XX).

In order to handle this condition gracefully, check the type of the object you get back from fetch_and_parse by wrapping it in something like:

unless feeds.is_a?(Fixnum)
  # work with the feeds object
else
  # handle the error condition, retry, etc.
end

You should also be able to see these failures by fetching the feed in a browser repeatedly, if it's frequent enough.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top