2010-10-05 25 views
0

我正在使用Ubuntu 10.04,我正在使用feed-zirra來解析RSS提要,並且我有MySQL數據庫。在Rails中進行分析

我想解析來自Times of India Top Stories的RSS提要。第一個鏈接似乎有問題,我相信TOI的人會很快糾正它。但無論如何,我不想在以後面對類似的錯誤,所以我想問你們如何解決這個問題。

單看這一點,並尤其是尋找鏈接

<item> 
    <title>CWG: Abhinav Bindra, Gagan Narang win first Gold for India</title 
    <description>Abhinav Bindra and Gagan Narang on Tuesday bagged Gold for the men's 10 m air rifle pair's event, getting India its first gold in the 19th Commonwealth Games.</description> 
<link>/cwgarticleshow/6688747.cms</link> 
<guid>/cwgarticleshow/6688747.cms</guid> 
<pubDate>Tue, 05 Oct 2010 04:57:46 GMT</pubDate> 
</item> 

的聯繫是<link>/cwgarticleshow/6688747.cms</link>

現在,當我點擊鏈接,在視圖中..其得到路由到http://localhost:3000/cwgarticleshow/6688747.cms而不是http://timesofindia.indiatimes.com/cwgarticleshow/6688747.cms

而我得到的錯誤是

**Routing Error** 

No route matches "/cwgarticleshow/6688747.cms" with {:method=>:get} 

如何更正此類型的錯誤?

期待您的幫助和支持

感謝

回答

0

你只需要預先http://timesofindia.indiatimes.com的鏈接標籤值,你會沒事的。

+0

我是新手。你能告訴我這是如何實現的嗎?我不明白? – gkolan 2010-10-05 06:21:55

0

您可以使用URI類。例如,您可以定義以下方法

require "uri" 

def repair_link(feed_link) 
    uri = URI.parse(feed_link) 
    uri.scheme ||= "http" 
    uri.host ||= "timesofindia.indiatimes.com" 
    uri.to_s 
end 

它會設置URL的方案和主機部分(如果它們尚未填充)。所以如果你打電話給正常的鏈接(如http://foo/bar.cms),那麼什麼都不會改變。

而最後一件事情 - 您可能應該在某處作爲異常捕獲異常,因爲#parse方法會在URI無效時引發InvalidURIError異常。但這取決於你如何處理它。