rails制作rss feed代码
网络编程 2021-07-05 10:37www.168986.cn编程入门
在网上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."
maker.items.do_sort = true
articles.each do |article|
item = maker.items.new_item
item.link = "http://.***./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./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
这种方法 网上很多文章到这一步就算完了,没有下文了,其实不对。到这一步,访问http://localhost:3000/post/feed仍然是出来的html的界面。g之,找到介绍:rails 2.0会根据不同的格式对模板进行渲染。这段代码放在/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."
maker.items.do_sort = true
articles.each do |article|
item = maker.items.new_item
item.link = "http://.***./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./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
这种方法 网上很多文章到这一步就算完了,没有下文了,其实不对。到这一步,访问http://localhost:3000/post/feed仍然是出来的html的界面。g之,找到介绍:rails 2.0会根据不同的格式对模板进行渲染。这段代码放在/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输出。
编程语言
- 机械手焊接机器人编程 机械手焊接设备编程
- vb数据库编程实例,vb做数据库
- tiobe世界编程语言排行榜,全球编程语言排行
- 成都java编程培训 成都it编程培训中心
- 编程基础培训,编程基础知识培训
- flash游戏编程基础教程,flash游戏开发教程
- 不会英语能学编程吗 不会英语可以学程序员吗
- 爱心代码编程C语言 爱心代码编程c语言公式
- c语言编程学习入门 c语言编程快速入门
- 西门子plc编程培训 西门子plc编程培训班
- 哪里可以学编程 台州哪里可以学编程
- 少儿编程哪个好 少儿编程哪个品牌好
- 编程是什么意思,图形编程是什么意思
- c语言编程软件下载,c语言编程软件在哪下载
- java编程软件下载,java编程下载手机版
- ug编程培训学校,ug编程培训教程