Loading feeds / entries getting undefined method 'entries' for 0:fixnum error

1k views Asked by At

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!

2

There are 2 answers

0
Kleber S. On BEST ANSWER

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!

1
sands On

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.