1

I've just started on Rails again and have recently followed the Sitepoint guide to building an RSS Feed app. I followed the instructions verbatim, but am getting the error undefined method 'html_safe' for nil:NilClass. I've looked into the database and it seems Feedjira is not parsing the content or author attributes into the database.

After a quick google, I found the following issues on the Github page #178 and #176 dated back from August 13, but given the significant issue this causes I'm surprised there's not on it. It may be an error in my code which can be found here but I've checked and don't think it is.

Does anyone know of anything I'm doing wrong? I have altered the self.feed_classes in the feedjira documentation as suggested in Issue #176 but it still doesn't pick anything up.

Dave C
  • 367
  • 5
  • 19

2 Answers2

1

RSS and Atom Feeds don't offer a content attribute. The 'content' is provided as 'summary' attribute. So change

local_entry.update_attributes(content: entry.content, author: entry.author, url: entry.url, published: entry.published)

to

local_entry.update_attributes(content: entry.summary, author: entry.author, url: entry.url, published: entry.published)

Nevertheless, NewsFeeds are very fickle things. So you shouldn't rely on the existence of single attributes.

Have a look at your show.html.erb

<%= @entry.content.html_safe %>

You could for example use the safe navigation operator

<%= @entry&.content&.html_safe %>

Or inform the user, that no content is present

<% unless @entry.content.nil? %>
    <%= @entry.content.html_safe %>
<% end %>
PascalTurbo
  • 2,189
  • 3
  • 24
  • 41
  • But presumably this will leave the main content out of the parse still? – Dave C Jan 14 '17 at 20:18
  • I'm sorry, what do you mean by this? – PascalTurbo Jan 14 '17 at 20:20
  • Well I want the main content from the news article, but I am not near my computer right now. I'm assuming the answer does not parse the content of the news article, but just displays an error if it doesn't? I've tried with a number of different common commercial newspaper feeds and none offer the content or author attribute. – Dave C Jan 14 '17 at 20:23
  • I've enhanced my answer - this should solve your problem. For the author: have a look at the feed - maybe it isn't present. – PascalTurbo Jan 14 '17 at 20:36
  • This has worked to a degree, but only gives a single line summary, not the actual content of a news report. Is this just a change to access to news articles? Will it just be a case of using Feedjira for the feed data and then using Nokogiri to scrape the url supplied? – Dave C Jan 15 '17 at 13:20
  • Feedjira only provides you with the content of the NewsFeed itself. If you need the whole article you'll have to download the content and than scrape it. – PascalTurbo Jan 16 '17 at 10:01
  • I thought as much, looks like it's all changed since the tutorial. Thanks! – Dave C Jan 16 '17 at 10:09
0

OK. I found where the problem was. I cloned your project and fired up. Apparently your show.html.erb page from feeds is different from the one from the article, that's why you couldn't access to entries:

Yours => From the article

If you want to see the entries first create new feed, with name and description, and for example add these links for each feed:

http://epijobs.com/rss.xml

http://feedjira.com/blog/feed.xml

When you finish adding feeds, from your terminal in root app, run cd lib/task and finally run: bundle exec rake sync:feeds as it mentioned from the article:

The rake task loops through all the Feeds stored in the database and fetches the latest content for each one. From that, loop through the new Entries, creating or updating it in the database. We are updating every time to keep up with any change in the source content.

Now, if you go to this link below for example, or through link show first feed:

http://localhost:3000/feeds/1/entries

You should be able to see all entries for feed id 1.

I hope it helps.

Blackcoat77
  • 1,574
  • 1
  • 21
  • 31
  • Still nothing unfortunately. – Dave C Jan 14 '17 at 15:24
  • I could always access this view. It was viewing the individual entry (`http://localhost:3000/feeds/1/entries/1`) which is the issue. – Dave C Jan 15 '17 at 09:26
  • Ah ok, now I see. You can simply check if value is present in DB, by adding <%= @entry.content.html_safe if @entry.content.present? %> that would solve your problem. – Blackcoat77 Jan 15 '17 at 15:35
  • Unfortunately not. The news story is what I want from the RSS feed, so if it's not there there is no point in having the feed. I'm not sure if all RSS feeds have changed now or if it's just something wrong with my code. – Dave C Jan 15 '17 at 16:31
  • I will try to explain you my scenario. I'm using exactly the same code from your repo. If I fetch this url [link](http://epijobs.com/rss.xml) and run task after that, and I go to see the content, I will get error like you got it. `'html_safe' for nil:NilClass`. If you go to that website you'll see that xml only has titles and links. That's why it it shows error. ( No content from source website ). If I add ` if @entry.content.present?` error will disappear, but still no content. If I fetch feedjira link I sent, it will show the content. I'm trying to understand you what you would like.. – Blackcoat77 Jan 16 '17 at 04:00
  • I cloned your repo, so we are working on the same stuff, unless you made some local changes and you haven't pushed to remote repo. From some websites you won't get content, because they didn't show xml properly on their side, like I showed you from both links from my answer – Blackcoat77 Jan 16 '17 at 04:05
  • This will be the final code from your `app/views/entries/show.html.erb` `

    <%= link_to @entry.title, @entry.url %>

    published on <%= @entry.published %> by <%= @entry.author %>

    <% if @entry.content.present? %> <%= @entry.content.html_safe %> <% else %>
    No content from the main source <% end %>

    `
    – Blackcoat77 Jan 16 '17 at 04:23
  • Ok, I understand that. But what I need is the content of news articles from RSS feeds. Is the best option to just use the URL and use nokogiri to scrape the content from the article? – Dave C Jan 16 '17 at 06:17
  • Yes. You can do it with Nokogiri. – Blackcoat77 Jan 16 '17 at 14:13