Quantcast
Channel: Getting invalid Nil Object Exception when creating RSS feed in Rails - Stack Overflow
Viewing all articles
Browse latest Browse all 2

Getting invalid Nil Object Exception when creating RSS feed in Rails

$
0
0

I'm creating an RSS feed for user notifications, each user has a feed unique to them which they get from a unique URL. The problem is that @notifications (set in the controller) is giving said to be a nil object, but @notifications both exists and contains data.

Here's the controller code:

def user_notifications
   render :layout => false 
   @user = User.find_by_api_key(params[:api_key], :limit => 1)
   @notifications = UserNotification.find_all_by_user_id(@user.id, :order => 'id DESC', :limit => 40)
   response.headers["Content-Type"] = "application/xml; charset=utf-8"
end

and this is the RXML file:

xml.instruct!

xml.rss "version" => "2.0", "xmlns:dc" => "http://purl.org/dc/elements/1.1/" do
xml.channel do

 xml.title       "MySite.com Notifications"
 xml.description "The latest goings on at your MySite.com"

 @notifications.each do |notification|
 xml.item do
   xml.title       notification.message.gsub(/<\/?[^>]*>/, "")
   xml.link        "http://www.MySite.com"+notification.url
   xml.description notification.message
   xml.guid        "http://www.MySite.com"+notification.url
 end
 end

end

end

When accessing the URL with the correct api_key, I get this error:

You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each

Extracted source (around line #9):

6:    xml.title       "MySite.com Notifications"
7:    xml.description "The latest goings on at your MySite.com"
8: 
9:    @notifications.each do |notification|
10:      xml.item do
11:        xml.title       notification.message.gsub(/<\/?[^>]*>/, "")
12:        xml.link        "http://www.MySite.com"+notification.url

The api_key given is correct, as shown under the Parameters on the Exception caught screen:

Parameters:

{"api_key"=>"AAEE5663HHH"}

When running the commands through the console everything works as expected (as when I run near identical code for displaying the notifications via a normal web page) so I suspect that @notifications isn't passed into the RXML file correctly, although I can't find out why not.

The User and API key definitely match, and there are definitely notifications for the user.

This is the first RSS I've made after following the example here: http://paulsturgess.co.uk/articles/show/13-creating-an-rss-feed-in-ruby-on-rails

Any help on this would be GREATLY appreciated!


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images