从Active Record 数据中生成 RSS Feesds

从Active Record 数据中生成 RSS Feesds

加入你的数据库中内容如下 :每条记录都含有经常变化的德销售信息。
db/schema.rb:

ActiveRecord::Schema.define() do
create_table "books", :force => true do |t|
t.column "title", :string, :limit => 80
t.column "sales_pitch", :string
t.column "est_release_date", :date
end
end


在XML控制器中创建一个叫rss 方法,这个方法从Book 模型中提取信息到实例变量。
app/controllers/xml_controller.rb:

class XmlController < ApplicationController

def rss
@feed_title = "O'Reilly Books"
@books = Book.find(:all, :order => "est_release_date desc",
:limit => 2)
end
end
在rss 动作关联的视图上。使用BUilder 的XML 标记来构建RSS
的XML。
app/views/xml/rss.rxml:

xml.instruct! :xml, :version=>"1.0", :encoding=>"UTF-8"
xml.rss('version' => '2.0') do
xml.channel do
xml.title @feed_title
xml.link(request.protocol +
request.host_with_port + url_for(:rss => nil))
xml.description(@feed_title)
xml.language "en-us"
xml.ttl "40"
# RFC-822 dateime example: Tue, 10 Jun 2003 04:00:00 GMT
xml.pubDate(Time.now.strftime("%a, %d %b %Y %H:%M:%S %Z"))
@books.each do |b|
xml.item do
xml.title(b.title)
xml.link(request.protocol + request.host_with_port +
url_for(:controller => "posts", :action => "show", :id => b.id))
xml.description(b.sales_pitch)
xml.guid(request.protocol + request.host_with_port +
url_for(:controller => "posts", :action => "show", :id => b.id))
end
end
end
end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值