0
所以我現在所擁有的是一個新聞控制器:Rails的引入nokogiri限制結果,並插入到數據庫
news_controller.rb
class NewsController < InheritedResources::Base
def new
require 'nokogiri'
doc = Nokogiri::XML(open("http://www.rollingstone.com/siteServices/rss/musicNewsAndFeature"))
@info = doc.xpath('//item').take(5).map do |i|
News.create(:title => i.xpath('title').inner_text, :description => i.xpath('description').inner_text, :link => i.xpath('link').inner_text)
end
end
end
...我可以在像這樣一個觀點呼籲:
更新與限制問題就在這裏: new.html.erb
<%= debug @info.each.first %>
應該是...
<%= debug @info %>
的 「each.first」 被強迫的控制器作爲整個事情。除了使用視圖外,還在尋找另一種保存到數據庫的方法。
這個工程,但我的問題是雙重的: 1.返回7050條新聞文章(我想要最新的5或10) 2.這不是我真正想做的。
我要的是一個Ruby腳本或耙任務是這樣的,我可以在命令行中運行:
Rails 3.0 Parsing XML and Inserting into Database
我只是不知道如何像這種格式。所以我想
- 檢索引入nokogiri
- 商店他們只是5個結果數據庫
我已經試過了。沒有工作。 @info = doc.xpath('// item')。take(5).map do | i |或者我必須放棄地圖?如果我這樣做,那麼我如何分類我的信息? –
我不確定你做了什麼。地圖只能循環這5個元素。它也應該導致5個元素。它在這裏很好。 – nurettin