Ruby on rails 在Rails中创建RSS提要时获取无效的Nil对象异常

Ruby on rails 在Rails中创建RSS提要时获取无效的Nil对象异常,ruby-on-rails,xml,rss,Ruby On Rails,Xml,Rss,我正在为用户通知创建一个RSS提要,每个用户都有一个唯一的提要,它们来自一个唯一的URL。问题是@notifications(在控制器中设置)是一个nil对象,但是@notifications既存在又包含数据 以下是控制器代码: def user_notifications render :layout => false @user = User.find_by_api_key(params[:api_key], :limit => 1) @notificatio

我正在为用户通知创建一个RSS提要,每个用户都有一个唯一的提要,它们来自一个唯一的URL。问题是@notifications(在控制器中设置)是一个nil对象,但是@notifications既存在又包含数据

以下是控制器代码:

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
这是RXML文件:

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
当通过控制台运行这些命令时,一切都按预期工作(就像我通过普通网页运行几乎相同的代码来显示通知一样),因此我怀疑@notifications没有正确地传递到RXML文件中,尽管我无法找到原因

用户和API密钥绝对匹配,并且用户肯定会收到通知

这是我在以下示例之后制作的第一个RSS:


在此方面的任何帮助都将不胜感激

您的问题是,在运行任何步骤之前,您正在控制器操作的顶部调用render

谢谢你!真不敢相信我错过了
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
Parameters:

{"api_key"=>"AAEE5663HHH"}