最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

rails制作rss feed代碼

 更新時(shí)間:2008年09月28日 12:06:59   作者:  
在網(wǎng)上Google了很多次,能找到用rails制作rss,但是總是找不到完整的。今天把完整的過(guò)程抄錄在這里
方法A:
就是你自己把RSS XML的格式拼湊好,輸出.并設(shè)置HTTP Header ,標(biāo)記content-type為application/XML,常見(jiàn)的代碼:
復(fù)制代碼 代碼如下:

#Post_controller::feed()
def feed
require "rss"
articles = Article.find :all, :order => 'post_date DESC', :limit => 10
feed = RSS::Maker.make("2.0") do |maker|
maker.channel.title = "Gang of Technology"
maker.channel.description = "Gang of Technology site"
maker.channel.link = "http://up-u.com"

maker.items.do_sort = true

articles.each do |article|
item = maker.items.new_item
item.link = "http://www.***.com/archives/#{article.id}"
item.title = article.title
item.date = article.post_date
item.description = ""
end
end
send_data feed.to_s, :type => "application/rss+xml", :disposition =>
'inline'
end

方法B:
Rails Controller->Action 代碼:

復(fù)制代碼 代碼如下:

#Post_controller::feed
def feed
@posts=Post.find :all,:limit=>”id desc”
end

erb模板:

xml.instruct!
xml.rss(”version”=>”2.0″,
“xmlns:dc”=>”http://purl.org/dc/elements/1.1/”) do
xml.channel do
xml.title “renlu.xu 's blog”
xml.link(url_for(:action=>”start”,:only_path=>false))
xml.description “My life My love”
xml.language “zh_CN”
xml.ttl 60

for event in @posts do
xml.item do
xml.title(event.title)
xml.description(event.body)
xml.pubDate(event.created_at.to_s(:rfc822))
xml.guid(event.id)
xml.link(”http://…..#{event.id}”)
end
end
end
end

這種方法 網(wǎng)上很多文章到這一步就算完了,沒(méi)有下文了,其實(shí)不對(duì)。到這一步,訪問(wèn)http://localhost:3000/post/feed仍然是出來(lái)的html的界面。g之,找到介紹:rails 2.0會(huì)根據(jù)不同的格式對(duì)模板進(jìn)行渲染。這段代碼放在/views/post/feed.rhtml中是沒(méi)有用的,需要放在/views/post/feed.atom.builder中,并且需要通過(guò)http://localhost:3000/post/feed/123.atom(這里123沒(méi)有什么實(shí)際意義 隨便抓的一個(gè)),或是http://localhost:3000/post/feed?format=atom才能正確地按rss+xml輸出。

相關(guān)文章

最新評(píng)論

屏东市| 徐汇区| 拜城县| 察雅县| 三河市| 长宁区| 阿坝| 耿马| 珲春市| 基隆市| 夏津县| 蚌埠市| 靖州| 西林县| 博兴县| 陇西县| 内丘县| 石嘴山市| 丰城市| 鄂伦春自治旗| 台北县| 台中县| 龙井市| 厦门市| 垫江县| 永春县| 龙里县| 南城县| 泌阳县| 阳城县| 隆回县| 明星| 射洪县| 芷江| 安乡县| 资讯 | 盐亭县| 九龙城区| 广东省| 江西省| 古田县|