rails制作rss feed代碼
更新時間:2008年09月28日 12:06:59 作者:
在網(wǎng)上Google了很多次,能找到用rails制作rss,但是總是找不到完整的。今天把完整的過程抄錄在這里
方法A:
就是你自己把RSS XML的格式拼湊好,輸出.并設置HTTP Header ,標記content-type為application/XML,常見的代碼:
#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 代碼:
#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)上很多文章到這一步就算完了,沒有下文了,其實不對。到這一步,訪問http://localhost:3000/post/feed仍然是出來的html的界面。g之,找到介紹:rails 2.0會根據(jù)不同的格式對模板進行渲染。這段代碼放在/views/post/feed.rhtml中是沒有用的,需要放在/views/post/feed.atom.builder中,并且需要通過http://localhost:3000/post/feed/123.atom(這里123沒有什么實際意義 隨便抓的一個),或是http://localhost:3000/post/feed?format=atom才能正確地按rss+xml輸出。
就是你自己把RSS XML的格式拼湊好,輸出.并設置HTTP Header ,標記content-type為application/XML,常見的代碼:
復制代碼 代碼如下:
#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 代碼:
復制代碼 代碼如下:
#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)上很多文章到這一步就算完了,沒有下文了,其實不對。到這一步,訪問http://localhost:3000/post/feed仍然是出來的html的界面。g之,找到介紹:rails 2.0會根據(jù)不同的格式對模板進行渲染。這段代碼放在/views/post/feed.rhtml中是沒有用的,需要放在/views/post/feed.atom.builder中,并且需要通過http://localhost:3000/post/feed/123.atom(這里123沒有什么實際意義 隨便抓的一個),或是http://localhost:3000/post/feed?format=atom才能正確地按rss+xml輸出。
相關文章
使用wordpress的$wpdb類讀mysql數(shù)據(jù)庫做ajax時出現(xiàn)的問題該如何解決
這篇文章主要介紹了使用wordpress的$wpdb類讀mysql數(shù)據(jù)庫做ajax時出現(xiàn)的問題該如何解決的相關資料,需要的朋友可以參考下2015-10-10關于Ajax中通過response在后臺傳遞數(shù)據(jù)問題
這篇文章給大家介紹了Ajax中通過response在后臺傳遞數(shù)據(jù)問題,需要的的朋友參考下吧2017-08-08Ajax bootstrap美化網(wǎng)頁并實現(xiàn)頁面的加載刪除與查看詳情
Bootstrap是Twitter推出的一個開源的用于前端開發(fā)的工具包,Bootstrap提供了優(yōu)雅的HTML和CSS規(guī)范,它即是由動態(tài)CSS語言Less寫成。下面通過本文給大家介紹Ajax bootstrap美化網(wǎng)頁并實現(xiàn)頁面的加載刪除與查看詳情,需要的朋友可以參考下2017-03-03用AJAX實現(xiàn)頁面登陸以及注冊用戶名驗證的簡單實例
下面小編就為大家?guī)硪黄肁JAX實現(xiàn)頁面登陸以及注冊用戶名驗證的簡單實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-10-10