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
任何幫助將不勝感激!
謝謝!不能相信我錯過了這一點 – SaucyK 2010-06-20 21:18:08