2010-06-20 36 views
0

我正在为用户通知创建一个RSS提要,每个用户都有一个唯一的提要,他们从唯一的URL获得唯一的提要。问题在于@notifications(在控制器中设置)给出了一个无对象,但@notifications都存在并且包含数据。在Rails中创建RSS提要时无效无对象异常

这里的控制器代码:

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 

在访问已正确API_KEY的URL,我得到这个错误:

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 

给出的API_KEY是正确的,下的参数如图所示,捕获到异常屏幕上:

Parameters: 

{"api_key"=>"AAEE5663HHH"} 

在通过Console一切正常运行的命令(如当我几乎相同的代码运行通过显示通知正常的网页),所以我怀疑@notifications没有正确传递到RXML文件中,尽管我找不到为什么。

用户和API密钥绝对匹配,并且肯定有用户通知。

这是第一个RSS我在这里下面的例子后提出:http://paulsturgess.co.uk/articles/show/13-creating-an-rss-feed-in-ruby-on-rails

任何帮助将不胜感激!

回答

1

你的问题是你在控制器动作的顶部调用渲染,在任何步骤运行之前!

+0

谢谢!不能相信我错过了这一点 – SaucyK 2010-06-20 21:18:08